在Kotlin中调用子例程



我似乎找不到有关子例程的任何信息以及如何在Kotlin中称呼它们。我是通过创建一个系统来练习的,该系统从用户那里获取输入,并显示内容,提示用户如果信息正确,或者否,则表示"是"。然后,它要求他们输入"重置"以输入子例程,直到他们输入是。

代码没有错误,但是当我进入"重置"阶段时,子例程从未被调用,并且程序只是悬挂。它使我能够输入其他内容,但后来终止程序。

这是以下代码:

fun main(args: Array<String>) {
    println("Enter your name")
    val name = readLine()
    println("Enter your email")
    val email = readLine()
    println("Enter your location")
    val location = readLine()
    println("$name $location $email")
    println("Are you sure this is the correct information about you? Type yes or no.")
    val answer = readLine()
    if (answer == "yes") {
        println("Awesome. Details have been saved.")
        return
    } else if (answer == "no") {
        println("Type reset to retry")
        val reset = readLine()
        if (reset == "reset") {
            loop()
        }
    } else {
        println("Error has occurred")
    }
}
fun loop (){
    val answer = readLine()
    while(answer == "no"){
        println("Enter your name")
        val name = readLine()
        println("Enter your email")
        val email = readLine()
        println("Enter your location")
        val location = readLine()
        println("$name $location $email")
        println("Are you sure this is the correct information about you? Type yes or no.")
        if(answer != "yes"){
            println("Awesome. Details have been saved.")
            break
        }
    }
}

做循环可以给您想要实现的效果,因此请尝试 -

fun loop (){
    do {
        println("Enter your name")
        val name = readLine()
        println("Enter your email")
        val email = readLine()
        println("Enter your location")
        val location = readLine()
        println("$name $location $email")
        println("Are you sure this is the correct information about you? Type yes or no.")
        val answer = readLine()
        if(answer == "yes"){
            println("Awesome. Details have been saved.")
            break
        }
    }
    while(answer == "no")
}

相关内容

  • 没有找到相关文章

最新更新