Last active
July 31, 2020 14:44
-
-
Save ochim/69502d062a29248cb1920c79ae577247 to your computer and use it in GitHub Desktop.
dice.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Android Basics: Add a button to an app | |
1.Classes and object instances in Kotlin | |
*/ | |
fun main() { | |
val myFirstDice = Dice(6) | |
println("Your ${myFirstDice.numSides} sided dice rolled ${myFirstDice.roll()}!") | |
val mySecondDice = Dice(20) | |
println("Your ${mySecondDice.numSides} sided dice rolled ${mySecondDice.roll()}!") | |
} | |
class Dice (val numSides: Int) { | |
fun roll(): Int { | |
return (1..numSides).random() | |
} | |
} | |
/* | |
3.Add conditional behavior in Kotlin | |
*/ | |
fun main() { | |
val myFirstDice = Dice(6) | |
val rollResult = myFirstDice.roll() | |
val luckyNumber = 4 | |
when (rollResult) { | |
luckyNumber -> println("You won!") | |
1 -> println("So sorry! You rolled a 1. Try again!") | |
2 -> println("Sadly, you rolled a 2. Try again!") | |
3 -> println("Unfortunately, you rolled a 3. Try again!") | |
4 -> println("No luck! You rolled a 4. Try again!") // useless condition | |
5 -> println("Don't cry! You rolled a 5. Try again!") | |
6 -> println("Apologies! you rolled a 6. Try again!") | |
} | |
} | |
class Dice(val numSides: Int) { | |
fun roll(): Int { | |
return (1..numSides).random() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment