gpt4 book ai didi

java - 从终端获取输入时出现不需要的回车

转载 作者:行者123 更新时间:2023-12-01 17:35:33 24 4
gpt4 key购买 nike

我刚刚开始学习 Java,遇到了一个小问题,我不知道如何解决。与 this 相同问题,但那里的解决方案在这里不起作用。那里的问题被认为是与 Netbeans 有关的结果,从命令行编译代码可能会解决问题,但我是从命令行编译并运行的。

基本上,当我运行以下代码时(为了简洁而进行了剪辑)

    System.out.println("Number: ");
line = in.readLine();

我得到以下输出:

Number:
//Input cursor is found here

我正在使用 Ubuntu 附带的 Gnome 终端运行代码。有谁知道这是什么原因造成的吗?

最佳答案

如果您不需要尾随换行符,请使用 print 而不是 println:

System.out.print("Number: ");
try {
line = in.readLine();
}

println 的 Javadocs 已经这么说了:

Terminate the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

关于java - 从终端获取输入时出现不需要的回车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6743819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com