samedi 26 décembre 2020

Coin Flip code in Kotlin - user input and if else

//I tried to write Coin flip code and want to compare user input with coin flip result. //and print Lucky and Unlucky.

fun main() {
    val myFirstDice = Coin()
    val diceRolled = myFirstDice.roll()
    println(""""Write your lucky Side! ex:(Tail or Head)""")

    val luckySide: String? = readLine()
    println(luckySide)

    if (diceRolled == luckySide) println("Lucky") else println("Unlucky")
}

class Coin {
    fun roll(): String {
    val coinSides = arrayOf("Tail", "Head")
    return coinSides[(coinSides.indices).random()]
    }

}

Aucun commentaire:

Enregistrer un commentaire