如果用户使用if / else在我的代码中写了除“是”,“否”之外的其他单词,我想再次提问。
您只能回答“是”或“否”,但回答如“ sjfkhs”会让我难过。如果有人写像“ sdjk”,我想重复这个问题。
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")
}
}
答案 0 :(得分:1)
在一般情况下,仅使用if / else是不可能的。一种选择是使用循环,而另一种选择是使用递归。我将演示如何做到这两个。我对您的代码进行了少许更改,以匹配标准的Kotlin命名约定和样式。如果您要参加某种课程,并且必须遵循该课程的特定约定,请忽略我的更改。这是更改后的问题代码:
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")
}
}
以下是通过递归解决此问题的方法:
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()
}
}
以下是使用while循环解决此问题的方法:
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
}
}
我个人更喜欢递归方法,尤其是因为kotlin的目标是成为一种更具功能性的语言。