if-statement - 我想回问用户是否用if/else在我的代码中写了 “Yes” “No”以外的单词

标签 if-statement kotlin

我想对用户是否使用if / else在我的代码中写“Yes”,“No”之外的其他单词再次提问。

您只能回答"is"或“否”,但回答如“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")
    }


}

最佳答案

在一般情况下,仅使用if / else是不可能的。一种选择是使用循环,而另一种选择是使用递归。我将演示如何做到这两个。我对您的代码进行了少许更改,以匹配标准的Kotlin命名约定和样式。如果您要参加某种类(class),并且必须遵循该类(class)的特定约定,请忽略我的更改。这是更改后的问题代码:

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的目标是成为一种更实用的语言。

关于if-statement - 我想回问用户是否用if/else在我的代码中写了 “Yes” “No”以外的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57633326/

相关文章:

c# - 棘手的 if 语句

javascript ->= 和 <= 标志在 thymeleaf 中出现错误

function - 确保kotlin方法是静态的,顶级的或带注释的@JvmStatic

gradle - 提供者 pactVerify 没有获取 JSON Pact 文件

javascript - 这个 if 语句可以是一个 switch 语句并且更漂亮或更简单吗?

java - 在Java中,如何让我的程序仅在前一个查询的值为true的情况下才询问下一个查询?

javascript - if 语句中的 JQuery 选择器

android-studio - 我可以在 Gradle 插件中配置 Kotlin 多平台模块吗?

android - 使用 CoroutineScope 的边界回调(Android 分页库)

kotlin - 可空WeakHashMap迭代期间的Kotlin空检查