Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
return a word from a given array in kotlin

I am beginner in kotlin (in coding in general) and am trying to get the following sentence: "The 6 sided [randomColor] dice rolled [randomNumber]". Basically the only part I cannot get correctly is the color, I get "kotlin.Unit" instead. The code I am using is below. Many thanks in advance for your help!

fun main() {
    val myFirstDice = Dice(6)
    println ("The ${myFirstDice.numSides} sided ${randomColor()} dice rolled ${myFirstDice.roll()}")
}
class Dice(val numSides: Int){ 
    fun roll(): Int{
        return (1..numSides).random()
    }
}
fun randomColor() {
    val list = listOf("black", "yellow", "green")
    val randomColor = list.shuffled().find { true }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I would do it a little bit differently. I would not use shuffled() method, but instead rely on the built-in random() method available since kotlin 1.3 (reference documentation):

fun randomColor(): String {
    val list = listOf("black", "yellow", "green")
    return list.random()
}
over 4 years ago · Santiago Trujillo Relatório

0

You need to return the value of randomColor:

fun randomColor(): String? {
    val list = listOf("black", "yellow", "green")
    val randomColor = list.shuffled().find { true }
    return randomColor;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda