为什么 readLine() 在我的 Kotlin 代码中不起作用?

发布于 2025-01-13 00:28:25 字数 621 浏览 6 评论 0原文

我有这段代码

fun main() {
    var input = "1.785 + 9755.44"
    val inputarr = input.split(" ")
    var output = inputarr[0].toDouble() + inputarr[2].toDouble()
    println(output)

    var a: String? = readLine()
    println(a)
}

,当我运行它时,它会执行到 print(output),但之后它就会停止,甚至不会停止运行。发生这种情况时,我被迫关闭 IDE 并重新启动。

我无法向 a 输入任何内容。

我一开始以为我的设置可能有问题,但是当我在 https://play.kotlinlang 上尝试时。 org/,我遇到了不同的问题。

我没有在 println(output) 部分后显示空白,而是在其后显示 null

知道问题出在哪里吗?

I have this block of code

fun main() {
    var input = "1.785 + 9755.44"
    val inputarr = input.split(" ")
    var output = inputarr[0].toDouble() + inputarr[2].toDouble()
    println(output)

    var a: String? = readLine()
    println(a)
}

When I run it, it executes up to print(output), but after that it just stops and doesn't even stop the running. I am forced to close the IDE and start again when this happens.

There is no way for me to input anything into a.

I thought at first that maybe something was wrong with my setup, but when I tried it on https://play.kotlinlang.org/, I got the different problem.

Instead of showing blank after the println(output) part, I got null after it.

Any idea what the problem could be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2025-01-20 00:28:25

由于 docreadLine () 返回:

如果输入流被重定向到文件并且已到达文件末尾,则读取该行或null

所以我想这取决于你的IDE如何管理输入流。我用 IntelliJ IDEA 尝试了你的代码,一切正常。

尝试检查您的 IDE 设置以更改其行为或尝试 IntelliJ IDEA。

Due to doc, readLine() returns:

the line read or null if the input stream is redirected to a file and the end of file has been reached.

So I guess it depends on your IDE to how to manage the input stream. I tried your code with IntelliJ IDEA and everything was OK.

Try checking your IDE settings to change its behavior or try IntelliJ IDEA.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文