在特定时间接收输入 [英] Receive input in specific time

查看:43
本文介绍了在特定时间接收输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个测试系统,我要做的就是计算用户在此问题上花费了多少秒.即我打印问题(标准System.out.println),然后等待5秒钟,如果用户在这5秒钟内(通过标准输入)回答了此问题,我想保留此值.

I'm writing a testing system and i all i want to do is to count how many seconds had user spent on this question. i.e. i print question(standard System.out.println), then wait 5 seconds and if within these 5 seconds user answered(through standard input), i want to keep this value.

如果用户在5秒钟内未提供答案,则必须跳过此问题并继续.

If user hasn't provided an answer in 5 seconds, it must skip this question and continue.

问题是我正在通过 Scanner 对象读取用户答案,我想像 in.nextInt()之类的东西是无法控制的.

The problem is I'm reading user answers via Scanner object, and something like in.nextInt() is uncontrollable, I suppose.

我该如何解决这个问题?这是我的代码片段,没有该功能,可以给我一些提示添加什么吗?

How can I solve this problem? Here is fragment of my code without that functionality, can you give me some hints what to add?

    public void start() {
    questions.prepareQuestions(numQuestions);
    Scanner in=new Scanner(System.in);
    boolean playerIsRight=false,botIsRight=false;
    int playerScore=0,botScore=0;
    for (int i = 0; i < numQuestions; i++) {
        questions.askQuestion(i);
        System.out.print("Your answer(number): ");
        playerIsRight=questions.checkAnswer(i,in.nextInt()-1); //in.nextInt() contains the answer
        botIsRight=botAnswersCorrectly(i + 1);
        if(playerIsRight){ playerScore++; System.out.println("Correct!");}
        else System.out.println("Incorrect!");
        if(botIsRight) botScore++;
        System.out.print("\n");
    }
    if(botScore>playerScore) System.out.println("Machine won! Hail to the almighty transistors!");
    else if(playerScore>botScore) System.out.println("Human won! Hail to the power of nature!");
    else System.out.println("Tie. No one ever wins. No one finally loses.");
}

推荐答案

在这种情况下,我将使用两个线程.主线程编写问题,等待答案,并保持得分.子线程读取标准输入并将答案发送到主线程,也许通过

I would use two threads in this case. The main thread writes questions, waits for answers, and keeps score. A child thread reads standard input and sends the answers to the main thread, perhaps via a BlockingQueue.

通过使用阻塞队列上的 poll()方法,主线程可以等待五秒钟:

The main thread can wait for five seconds for an answer by using the poll() method on the blocking queue:

…
BlockingQueue<Integer> answers = new SynchronousQueue();
Thread t = new ReaderThread(answers);
t.start();
for (int i = 0; i < numQuestions; ++i) {
  questions.askQuestion(i);
  System.out.print("Your answer (number): ");
  Integer answer = answers.poll(5, TimeUnit.SECONDS);
  playerIsRight = (answer != null) && questions.checkAnswer(i, answer - 1); 
  …
}
t.interrupt();

如果此调用返回 null ,则主线程知道子线程在此期间未收到任何输入,并且可以适当地更新分数并打印下一个问题.

If this call returns null, the main thread knows that the child thread didn't receive any input during that time, and can update the score appropriately and print the next question.

ReaderThread 看起来像这样:

class ReaderThread extends Thread {

  private final BlockingQueue<Integer> answers;

  ReaderThread(BlockingQueue<Integer> answers) { 
    this.answers = answers; 
  }

  @Override 
  public void run() {
    Scanner in = new Scanner(System.in);
    while (!Thread.interrupted()) 
      answers.add(in.nextInt());
  }

}

System.in 上使用, Scanner 将一直阻塞,直到用户按下 Enter ,这样用户可能已经输入了一些当主线程超时并移至下一个问题时,输入文本,但尚未按 Enter .用户将必须删除他们的待处理条目,并为新问题输入新答案.我不知道如何解决这种尴尬,因为没有可靠的方法来中断 nextInt()调用.

Used on System.in, the Scanner will block until the user presses Enter, so it might happen that the user has entered some text but not yet pressed Enter when the main thread times out and moves on to the next question. The user would have to delete their pending entry and enter a new answer for the new question. I don't know of a clean way around this awkwardness, since there's not a reliable way to interrupt the nextInt() call.

这篇关于在特定时间接收输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆