Java 用户输入而不打印换行符
在 Java 中,我如何接受用户输入而不打印换行符。我想给出提示并在输入无效时用回车重置光标。 Console.readLine Scanner.nextInt 和 BufferedReader.read 似乎都在按下 Enter 键时打印换行符。
我希望当用户按 Enter 提交输入时光标保持不动。
谢谢。
How, in Java, can I accept user input without printing a newline. I want to give a prompt and reset the cursor with a carriage return if the input is invalid. Console.readLine Scanner.nextInt and BufferedReader.read all seem to print a newline when enter is pressed.
I want the cursor to stay put when the user presses enter to submit input.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须重新绘制整个屏幕,或者使用curses 库。
对不起。我知道这不是您想听到的,但它们是中断...这通常是控制台的限制,其设计目的是允许“哑终端”与“智能服务器”进行通信,并且 I/O 通常是在“逐行”的基础。
所以你明白为什么我们现代程序员只是喜欢 GUI、HTML 和基本上所有非控制台的东西了……因为控制台应用程序编写起来非常痛苦,而且它们看起来也像 shazbut ;- )
干杯。基思.
You'll have to repaint the whole screen, or use a curses library.
Sorry. I know that's not what you wanted to hear, but them's the breaks... It's a limitation of Consoles generally, which were designed to allow "dumb terminals" to communicate with a "smart server", and I/O was typically done on a "line by line" basis.
So you see why us modern programmers just love GUI's, HTML, and basically all things non-console... because console applications where complete, utter, total pain-in-the-arse to write, and they looked like shazbut too ;-)
Cheers. Keith.