Practice Time: getFortuneCookie() - Version 2
Practice Time:
Loops
This lesson introduced the while and repeat loops. To practice using them, do the following:
- Change your fortune cookie program to use
repeat()
instead of afor
loop. What happens to the break instruction? Using the error message from the compiler, with what you've learned so far, can you think of why? - Change your fortune cookie program to use a
while
loop, which is the better choice when you are looping until a condition is met.
/** File: Main.kt
* Description: Create a program with a function that returns a fortune
* cookie message that you can print.
* Programmer: Nolverto Urias Obesoo
* Creation Date: 06/13/2023
*/
fun main(args: Array<String>) {
// Use a for loop to run the program 10 times
// for (iteration in 0..9){
// println("Your fortune is: ${getFortuneCookie(getBirthday())}\n")
// }
// Use repeat() instead of a for loop to run the program 10 times
// repeat(10){
// println("Your fortune is: ${getFortuneCookie(getBirthday())}\n")
// }
// Change your fortune cookie program to use a while loop, which is the better choice
// when you are looping until a condition is met.
var iteration: Int = 0
while(iteration < 10){
println("Your fortune is: ${getFortuneCookie(getBirthday())}\n")
iteration++
}
}
fun getFortuneCookie(birthday: Int): String{
val fortuneList = listOf(
"You will have a great day!",
"Things will go well for you today.",
"Enjoy a wonderful day of success.",
"Be humble and all will turn out well.",
"Today is a good day for exercising restraint.",
"Take it easy and enjoy life!",
"Treasure your friends because they are your greatest fortune.")
// You can use toIntOrNull() to convert a number as a String to an Integer numeric.
// If the user enters "", toIntOrNull returns null.
// The next line gets the indexFortune calculating the reminder beteen the birthday and size of fortuneList
var indexFortune: Int? = birthday.mod(fortuneList.size)
when(birthday){
in 28..31 -> indexFortune = 0
else -> indexFortune = birthday.mod(fortuneList.size)
}
return fortuneList[indexFortune!!]
}
fun getBirthday(): Int{
print("Enter your birthday: ")
// Use readLine() to read a line of input (completed with Enter) as a String.
val birthday = readLine()!!
return birthday.toInt()
}
Output:
Enter your birthday: 1 Your fortune is: Things will go well for you today. Enter your birthday: 2 Your fortune is: Enjoy a wonderful day of success. Enter your birthday: 3 Your fortune is: Be humble and all will turn out well. Enter your birthday: 4 Your fortune is: Today is a good day for exercising restraint. Enter your birthday: 5 Your fortune is: Take it easy and enjoy life! Enter your birthday: 6 Your fortune is: Treasure your friends because they are your greatest fortune. Enter your birthday: 7 Your fortune is: You will have a great day! Enter your birthday: 8 Your fortune is: Things will go well for you today. Enter your birthday: 9 Your fortune is: Enjoy a wonderful day of success. Enter your birthday: 10 Your fortune is: Be humble and all will turn out well. Process finished with exit code 0
Comments
Post a Comment