在Kotlin中调用子程序

我似乎无法find关于子程序的任何信息,以及如何在Kotlin中调用它们。 我正在通过创建一个系统,从用户接收输入,并显示提示用户如果他们的信息是正确的说“是”的内容,或者如果没有,则显示内容。 然后要求他们输入“reset”进入子程序,直到他们输入yes。

代码没有错误,但是当我进入“重置”阶段时,子程序永远不会被调用,程序只是挂起。 它允许我输入其他的东西,但只是结束程序。

这是下面的代码:

fun main(args: Array) { 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") }