Reputation: 21
I Want to return to question if user write other words than "Yes" "No" in my code with if/else.
You only answer with "YES" or "NO" but answer like"sjfkhs" make me sad. if somebody write like "sdjk" i want to repeat the question.
fun main(args: Array<String>) {
var CanI_Play: String
var IfUrMotherDisturb: String
var HaveYouFreeTime: String
var Question1 = "Can I play?"
println(Question1)
CanI_Play = readLine()!!.toUpperCase() //wstawienie !!. mówi komputerowi , że nie wprowadzimy null
// czyli chyba pustego pola które w tym przypadku bedzie zmienione z małych liter na duże
if (CanI_Play=="YES") {
println("So you can play from now")
}
if (CanI_Play=="NO") {
println("You can't play yet...:(")
println("Have You free time?")
HaveYouFreeTime = readLine()!!.toUpperCase()
if (HaveYouFreeTime=="NO") {
println("Do what You should do and You can play")
}
if (HaveYouFreeTime=="YES") {
println("If Your mother disturb?")
IfUrMotherDisturb = readLine()!!.toUpperCase()
if (IfUrMotherDisturb=="YES"){
println("Bad news. Time to look for new house. OMFG")
}
if (IfUrMotherDisturb=="NO"){
println("Great news! You can play!")
}
else{
println("I Want to return to question IfUrMotherDisturb")
}
}
else{
println("I want to return to question HaveYouFreeTime")
}
}
else{
println("I want to return to question CanI_Play")
}
}
Upvotes: 2
Views: 103
Reputation: 2016
This is not possible in the general case using only if/else. One option would be to use a loop, while another would be to use recursion. I will demonstrate how to do both. I have slightly changed your code to match standard Kotlin naming conventions and style. If you are taking some kind of class, and you are required to follow a specific convention for that class, disregard my changes. Here is the changed question code:
fun main() {
println("Can I play?")
val canIPlay = readLine()!!.toUpperCase()
if (canIPlay == "YES") {
println("So you can play from now")
}
if (canIPlay == "NO") {
println("You can't play yet...:(")
println("Have You free time?")
val haveYouFreeTime = readLine()!!.toUpperCase()
if (haveYouFreeTime == "NO") {
println("Do what You should do and You can play")
}
if (haveYouFreeTime == "YES") {
println("If Your mother disturb?")
val ifUrMotherDisturb = readLine()!!.toUpperCase()
if (ifUrMotherDisturb == "YES") {
println("Bad news. Time to look for new house. OMFG")
}
if (ifUrMotherDisturb == "NO") {
println("Great news! You can play!")
} else {
println("I Want to return to question IfUrMotherDisturb")
}
} else {
println("I want to return to question HaveYouFreeTime")
}
} else {
println("I want to return to question CanI_Play")
}
}
Here is how to solve this problem with recursion:
fun main() {
askIfCanPlay()
}
fun askIfCanPlay() {
println("Can I play?")
val canIPlay = readLine()!!.toUpperCase()
if (canIPlay == "YES") {
println("So you can play from now")
}
if (canIPlay == "NO") {
askIfFreeTime()
} else {
askIfCanPlay()
}
}
fun askIfFreeTime() {
println("You can't play yet...:(")
println("Have You free time?")
val haveYouFreeTime = readLine()!!.toUpperCase()
if (haveYouFreeTime == "NO") {
println("Do what You should do and You can play")
}
if (haveYouFreeTime == "YES") {
askIfMotherDisturb()
} else {
askIfFreeTime()
}
}
fun askIfMotherDisturb() {
println("If Your mother disturb?")
val ifUrMotherDisturb = readLine()!!.toUpperCase()
if (ifUrMotherDisturb == "YES") {
println("Bad news. Time to look for new house. OMFG")
}
if (ifUrMotherDisturb == "NO") {
println("Great news! You can play!")
} else {
askIfMotherDisturb()
}
}
Here is how to solve this problem with while loops:
fun main() {
var canIPlay = ""
while(!(canIPlay == "YES" || canIPlay == "NO")){
println("Can I play?")
canIPlay = readLine()!!.toUpperCase()
}
if (canIPlay == "YES") {
println("So you can play from now")
}
else if (canIPlay == "NO") {
println("You can't play yet...:(")
var haveYouFreeTime = ""
while(!(haveYouFreeTime == "YES" || haveYouFreeTime == "NO")){
println("Have You free time?")
haveYouFreeTime = readLine()!!.toUpperCase()
}
if (haveYouFreeTime == "NO") {
println("Do what You should do and You can play")
}
if (haveYouFreeTime == "YES") {
var ifUrMotherDisturb = ""
while(!(ifUrMotherDisturb == "YES" || haveYouFreeTime == "NO")){
println("If Your mother disturb?")
ifUrMotherDisturb = readLine()!!.toUpperCase()
}
if (ifUrMotherDisturb == "YES") {
println("Bad news. Time to look for new house. OMFG")
}
if (ifUrMotherDisturb == "NO") {
println("Great news! You can play!")
} else {
assert(false)// this will never happen
}
} else {
assert(false)// this will never happen
}
}else {
assert(false)//this will never happen
}
}
I personally prefer the recursive method, especially since kotlin aims to be a more functional language.
Upvotes: 1