简体   繁体   English

在文本不断打印到控制台时读取用户输入(无诅咒?)

[英]Read user input while text is constantly printing to console (Without curses?)

I'm trying to print text to the console while at the same time, reading user input. 我同时尝试在读取用户输入的同时将文本打印到控制台。 But every time I do that while typing the user input usually gets cut in half by output. 但是每次我这样做的时候,用户输入通常都会被输出切成两半。 I would rather not use curses as an option because that just seems overkill for what I'm trying to do, as I would only be using it to grab user input. 我宁愿不使用curses作为选项,因为这似乎对我尝试做的事情来说是过高的,因为我只会使用它来获取用户输入。

[Log] This is a log 
input

[Log] This inpis a log 
ut

I've also seen a Windows only solution, but that doesn't help me much. 我还看到了仅Windows的解决方案,但这并没有太大帮助。 Are there any libraries to do this in a multiplatform way? 是否有任何库可以以多平台方式执行此操作?

To do this using Java, you are going to need to use Threading . 要使用Java做到这一点,您将需要使用Threading

Java Threading docs . Java线程文档

Oracle concurrency tutorial . Oracle并发教程

Check out this similar answer , I think it will put you in the right direction: 看看这个类似的答案 ,我认为它将为您指明正确的方向:

Basically , you will have your main program running, and a Thread running. 基本上 ,您将运行您的主程序和一个Thread。 They will run simultaneously. 它们将同时运行。 I think it'd be simplest to have the Thread do the printing, and your main program do the input. 我认为让Thread进行打印是最简单的,而主程序则可以进行输入。

One caveat: If you want your program to be able to print different things (not just repeat the same println()), you may need to stop the previous Thread and kick off another when you want it to say different things. 一个警告:如果您希望您的程序能够打印不同的内容(而不仅仅是重复相同的println()),则可能需要停止上一个线程,并在您希望它说不同的内容时启动另一个线程。

I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM