我似乎无法找到有关子程序的任何信息以及如何在Kotlin中调用它们。我正在练习创建一个系统,该系统从用户那里获取输入并显示提示用户在信息正确时说“是”的内容,如果不是,则显示“否”。然后它要求他们输入“reset”进入子程序,直到他们输入yes。
代码没有错误,但是当我进入“重置”阶段时,子程序永远不会被调用,程序就会挂起。它允许我输入其他东西,但然后只是结束程序。
以下是代码:
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
}
}
}
答案 0 :(得分:0)
while循环可以提供你想要达到的效果,所以试试这个 -
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")
}