首页 > 解决方案 > 为什么这个while循环在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.")
        }
    }
}

标签: kotlin

解决方案


您需要将这些东西放在while循环中以在控制台中多次显示

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

推荐阅读