为什么这个while循环在Kotlin中只运行一次?

时间:2020-07-04 04:04:15

标签: kotlin

package search
import java.util.*
import kotlin.system.exitProcess

fun main() {
    val scanner = Scanner(System.`in`)

    println("Enter the number of people: ")
    val datasetSize = scanner.nextInt()

    println("Enter all people: ")
    val dataset = Array<String>(datasetSize){readLine()!!}
    println("===Menu===")
    println("1. Find a person")
    println("2. Print all people")
    println("0. Exit")
    var choice = scanner.nextInt()
    while(true){
        when(choice) {
            1 -> find(datasetSize,dataset)
            2 -> listp(datasetSize,dataset)
            0 -> exitProcess(-1)
            else -> println("Incorrect option! Try again.")
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您需要将这些内容放入while循环中,才能在控制台中多次显示

println("===Menu===")
println("1. Find a person") 
println("2. Print all people") 
println("0. Exit") 
var choice = scanner.nextInt()

答案 1 :(得分:0)

我想您希望菜单多次运行,因此您必须移动while以包括菜单逻辑。 有趣的main(){ val扫描仪=扫描仪(系统。in

println("Enter the number of people: ")
val datasetSize = scanner.nextInt()

println("Enter all people: ")
val dataset = Array<String>(datasetSize){readLine()!!}
while(true){
    println("===Menu===")
    println("1. Find a person")
    println("2. Print all people")
    println("0. Exit")
    var choice = scanner.nextInt()
    when(choice) {
        1 -> find(datasetSize,dataset)
        2 -> listp(datasetSize,dataset)
        0 -> exitProcess(-1)
        else -> println("Incorrect option! Try again.")
    }
}

}

相关问题