Problem with Kotlin readLine() function using IntelliJ internal console

时光怂恿深爱的人放手 提交于 2020-12-13 06:31:38

问题


When I try to read some value from a user, I get strange behavior.

For example, if I have one simple program:

fun main() {
    print("insert value: ")
    val tmp = readLine()
    println("value = $tmp")
}

I would expect the next behavior of the program:

insert value: 1
value = 1

But I get the next behavior:

insert value: 1
1
value = 1

So I would expect to insert the value 1, hit Enter, and the program would output value = 1. But instead of this, I have to input the value 1, hit Enter, input the value 1, hit Enter, and then I get the desired output.

Is there any option to run the Kotlin program in an external console instead of the IntelliJ internal console? Because I updated IntelliJ to the latest version and assume that maybe there is a problem with the new version?


回答1:


on your Kotlin REPL

   fun main() {
        print("insert value: ")
        val tmp = readLine()
        println("value = $tmp")
    }
    main()

Then to execute you press Ctrl+Enter

You get:

insert value: 

then insert the value 1 and press Ctrl+Enter one more time to execute.

and the output is:

insert value: 1
value = 1


来源:https://stackoverflow.com/questions/65019899/problem-with-kotlin-readline-function-using-intellij-internal-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!