Java 用户输入而不打印换行符

发布于 2024-11-05 00:54:13 字数 178 浏览 6 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(1

东走西顾 2024-11-12 00:54:13

您必须重新绘制整个屏幕,或者使用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.

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