Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If statement 我想回到这个问题,如果用户写的不是“quot;是”&引用;否";在我的代码中使用if/else_If Statement_Kotlin - Fatal编程技术网

If statement 我想回到这个问题,如果用户写的不是“quot;是”&引用;否";在我的代码中使用if/else

If statement 我想回到这个问题,如果用户写的不是“quot;是”&引用;否";在我的代码中使用if/else,if-statement,kotlin,If Statement,Kotlin,我想回到问题,如果用户在我的代码中用if/else写“是”“否”以外的词 你只回答“是”或“不是”,但像“sjfkhs”这样的回答让我很难过。如果有人写得像“sdjk”,我想重复这个问题 fun main(args: Array<String>) { var CanI_Play: String var IfUrMotherDisturb: String var HaveYouFreeTime: String var Question1 = "Can I pl

我想回到问题,如果用户在我的代码中用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")
    }


}
fun main(args:Array){
var CanI_Play:弦乐
变量ifurMotherDistrict:字符串
var HaveYouFreeTime:String
var Question1=“我可以玩吗?”
println(问题1)
CanI_Play=readLine()!!.toUpperCase()//wstavienie!!.mówi komputerowi,że wprowadzimy null
//czyli chyba pustego pola które w tym przypadku bedzie zmienione z małych l na duże
如果(CanI_播放=“是”){
println(“这样你就可以从现在开始玩了”)
}
如果(CanI_播放=“否”){
println(“你还不能玩…:(”)
println(“你有空吗?”)
HaveYouFreeTime=readLine()!!.toUpperCase()
如果(HaveYouFreeTime==“否”){
println(“做你应该做的,你可以玩”)
}
如果(HaveYouFreeTime==“是”){
println(“如果你妈妈打扰了?”)
IfurMotherDistribut=readLine()!!.toUpperCase()
如果(IfurMotherDistribut==“是”){
println(“坏消息。该找新房子了。OMFG”)
}
如果(IfurMotherDistribut==“否”){
println(“好消息!你可以玩了!”)
}
否则{
println(“我想回到问题ifurmotherdistrip”)
}
}
否则{
println(“我想回到问题HaveYouFreeTime”)
}
}
否则{
println(“我想回到问题CanI_Play”)
}
}

在一般情况下,仅使用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的目标是成为一种功能性更强的语言。

Offtopic:when:为了防止由于太深的递归而导致堆栈溢出,kotlin还提供了
tailrec
,请参阅Agreed。但是我没有提到tailrec或递归防护,因为s似乎是一个初学者问题,不想让事情变得过于复杂。
tailrec
修饰符使Kotlin中的递归比Java中的递归“更具功能”,事实上,编译器会将您的第一个解决方案转换为两个
do…而
循环,如果您将它们标记为
tailrec
,那么没有它第二种方法似乎更好。