高级 Java 控制台输入
作为一种自我练习,我正在用 Java 创建一个 BF 解释器,它通过控制台。在大多数情况下,这很好,但是,正如您可能知道的那样,BF 一次接受一个字符的输入,包括换行符。这意味着据我所知,标准控制台输入库将不起作用,因为它们接受整个字符串,然后在最后要求返回。我希望 ,
接受适合 char
的下一个击键,在键入时不打印它。因此,当 BF 要求输入并且您输入 a
时,它会接收 a
但不会在控制台中显示它,除非 BF 程序输出 a稍后再说。
我该怎么做?
As a self-practice, I'm making a BF interpreter in Java that does I/O through the console. For the most part it's fine, however, as you might be aware, BF takes input one character at a time, including newlines. This means that as far as I know the standard console input libraries won't work, because they take in entire strings, and then ask for a return at the end. I want ,
to take in the next keystroke that fits into a char
without printing it when it is typed. So when BF asks for input, and you type in a
, it takes in the a
but doesn't show it in the console unless the BF program outputs a
itself later.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用 JLine http://jline.sourceforge.net
Terminal
类有一个disableEcho()
方法来执行您想要的操作。I suggest using JLine http://jline.sourceforge.net
The
Terminal
class has adisableEcho()
method to do what you want.使用框架中的 textArea 和 keyListener 编写您自己的控制台输出/输入
Write your own console output/input using a textArea in a frame and a keyListener
查看
java.io.Console
- 您可以使用readPassword()
读取不带回显的字符串。但它仍然是面向行的,因此在用户按 Enter 之前您不会收到任何内容。另一个可能的缺点是可能没有控制台 - 取决于应用程序的启动方式。Check out
java.io.Console
- you can read a string w/o echo withreadPassword()
. It is still line oriented though, so you won't receive anything until user presses Enter. Another possible disadvatage is that there may be no console - depends on the way app is launched.