Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

384
Views
En kotlin, ¿hay alguna manera de 'lanzar un dado' cada vez que se baraja una matriz?

aquí hay un código de muestra y lo que estoy tratando de hacer... Quiero recorrer su lista hasta que se muestre cada uno de los elementos de la matriz, se muestra al hacer clic en el problema es que las variables d6, d62 y d63 son solo se 'enrolló' una vez al abrir el programa, incluso si pongo esas variables en la función de reproducción aleatoria, todavía no las volverá a colocar dentro de la lista porque la lista se copia en una nueva lista al inicio del programa ... ¿cómo puedo tirar los dados cada vez que se baraja la lista?

 var countW = 0 var d6 = Random().nextInt(6) + 1 var d62 = Random().nextInt(6) + 1 var d63 = Random().nextInt(6) + 1 var testList = arrayListOf( "test $d6", "test $d62", "test $d63") var shuffledList = testList.shuffled() fun shuf() { shuffledList = testList.shuffled() } listButton.setOnClickListener { if (countW < 1) { shuf() } wResult.text = shuffledList.elementAt(countW) countW++ if (countW >= shuffledList.count()) { countW = 0 } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No estoy seguro de lo que quieres exactamente. Pero una cosa está clara: los elementos de su lista se establecen una vez en:

 var testList = arrayListOf( "test $d6", "test $d62", "test $d63")

Cambiar d6, d62 y d63 no actualiza cada elemento de cadena de su ArrayList testList.

No sé si saber eso resuelve su problema, pero aquí hay un pequeño fragmento de código (bastante simplista) que puede hacerle entender, ¿sería ese su problema?

 /** * You can edit, run, and share this code. * play.kotlinlang.org */ import java.util.Random fun main() { // var countW = 0 var d6 = Random().nextInt(6) + 1 var d62 = Random().nextInt(6) + 1 var d63 = Random().nextInt(6) + 1 var testList = arrayListOf( "test $d6", "test $d62", "test $d63"); fun reRand(){ d6 = Random().nextInt(6) + 1 d62 = Random().nextInt(6) + 1 d63 = Random().nextInt(6) + 1 testList = arrayListOf( "test $d6", "test $d62", "test $d63"); } var shuffledList = testList.shuffled(); fun shuf() { shuffledList = testList.shuffled() } shuf() reRand() println(shuffledList) shuf() reRand() println(shuffledList) shuf() reRand() println(shuffledList) shuf() reRand() println(shuffledList) }

Necesita regenerar su lista cada vez, si así es como quiere hacerlo. Hay formas más eficientes de hacerlo.

Editar: aquí hay un enlace al patio de juegos de Kotlin: https://pl.kotl.in/xRdGELkDb

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!