在 Kotlin 中调用子程序
Calling Subroutines in Kotlin
我似乎找不到任何关于子例程以及如何在 Kotlin 中调用它们的信息。我正在通过创建一个系统来练习,该系统接受用户的输入并显示提示用户的内容,如果他们的信息正确则说是,如果不正确则说否。然后要求他们输入 "reset" 进入子程序,直到他们输入 yes。
代码没有错误,但是当我进入 "reset" 阶段时,子例程从未被调用,程序只是挂起。它允许我输入其他内容,但随后就结束了程序。
下面是代码:
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
}
}
}
做 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")
}
我似乎找不到任何关于子例程以及如何在 Kotlin 中调用它们的信息。我正在通过创建一个系统来练习,该系统接受用户的输入并显示提示用户的内容,如果他们的信息正确则说是,如果不正确则说否。然后要求他们输入 "reset" 进入子程序,直到他们输入 yes。
代码没有错误,但是当我进入 "reset" 阶段时,子例程从未被调用,程序只是挂起。它允许我输入其他内容,但随后就结束了程序。
下面是代码:
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
}
}
}
做 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")
}