如果用户在一段时间内没有问题(例如10秒),那么程序应该在控制台(Eclipse IDE)中与用户进行对话,程序在控制台中询问,程序应该询问下一个。
如果用户没有输入任何内容,如何在控制台中关闭输入? 执行示例:
programm print: "What is your name?"
user can put: "Bond"
programm print: "How old are you?"
user can put: "25"
(在这种情况下一切都很好)
但我对这种情况有疑问:
programm print: "What is your name?"
user put anything
programm print: "How old are you?"
user can put: "25"
然后在那个控制台上激活了我可以放一行。如果用户将忽略100个问题,例如,控制台将提供100行,如何解决这个问题?
我的代码如下:
...
static answer;
...
// 5 questions for example
for (int i = 0; i < 5; i++) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
final Thread timeForAnswer = new Thread(() -> {
Thread.sleep(10000);
}).start();
final Thread input = new Thread(() -> {
answer = reader.readLine();
// If user put answer before 10 sec. period - interrupt timeForAnswer and ask next question
timeForAnswer.interrupt();
}).start();
timeForAnswer.join();
input.interrupt();
System.out.println("Answer: " + answer);
}