Java:使用计时器进行测验

标签 java

我正在尝试实现用户必须在测验中回答问题的时间限制。尽管我在计时器上找到了很多东西,但我不知道如何将它们拼凑在一起。

我希望用户有 15 秒的时间来回答问题。如果他们及时回答,它会检查答案是否正确,然后询问他们是否要继续下一个问题。

如果用户在 15 秒内没有给出任何答复,那么它应该说答案不正确,并让他们可以选择转到下一个问题。

这是我到目前为止所拥有的。

for(int i=0; i<quiz.getQuizQuestions().size(); i++){

        super.getQuestion(i);

        //While timer is less than 15 seconds

        getResponse(i, questionStart);

        //If time has run out output "You have run out of time"

        super.nextQuestion();


}

这可能值得了解:

super.getQuestion(i) 只是打印所提出的问题

getResponse() 正在等待键盘输入。如果输入了某些内容,则会检查用户是否正确。

super.nextQuestion() 询问用户是否要转到下一个问题

提前致谢

编辑:如果在将其转换为 GUI 时能够轻松实现从 15 开始倒数的计数器,那也将是令人惊奇的。

最佳答案

使用 ExecutorService 和 Future 来确保我们读取一行或中断它。代码比我预期的要长一些...如果有不清楚的地方请告诉我:

import java.util.concurrent.*;
import java.io.*;    

public class Test {
    public static void main(String[] args) throws java.io.IOException {
        Question q = new Question();
        System.out.println("You have 5 seconds: " + q.toString());

        String userAnswer = null;    
        ExecutorService ex = Executors.newSingleThreadExecutor();
        try {
          Future<String> result = ex.submit(new GetInputLineCallable());
          try {
            userAnswer = result.get(5, TimeUnit.SECONDS);
            if (Integer.valueOf(userAnswer) == q.getAnswer()){
                System.out.println("good!");
            }
            else{
                System.out.println("Incorrect!");
            }

          } catch (ExecutionException e) {
            e.getCause().printStackTrace();
          } catch (TimeoutException e){
            System.out.println("too late!");
            return;
          } catch (InterruptedException e){
            System.out.println("interrupted?");
            e.getCause().printStackTrace();
          }

        } finally {
          ex.shutdownNow();
        }
    }
}



class GetInputLineCallable implements Callable<String> {
  public String call() throws IOException {
    BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
    String input = "";
    while ("".equals(input)) {
      try {
        while (!inp.ready()) {
          Thread.sleep(100);
        }
        input = inp.readLine();
      } catch (InterruptedException e) {
        return null;
      }
    } 
    return input;
  }
}




class Question{
  int p1, p2;
  public Question(){
    p1 = 2;
    p2 = 3;
  }
  public String toString(){
    return String.format("%d + %d = ?", p1, p2);
  }
  public int getAnswer(){
    return p1+p2;
  }  
}

关于Java:使用计时器进行测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7909642/

相关文章:

java - 在 Java 程序中创建和销毁多个 AWT-Shutdown 和 AWT-EventQueue 线程

java - 本地驱动器中图像的路径

java - 将mysql数据库中的记录显示到java中的自定义Jtable

java - android 新手 - <integer> 语法令人困惑

java - 关于在 3 ignite 服务器集群上使用 sql 查询获取数据的问题

java - 为什么 JVM 不断进行 full gc,而老一代只有半满?

java - 如果另一个应用程序在存储(DB)上更改了数据,如何触发应用程序更新缓存?

java - 表达式开始非法 : declaring a method inside another method

java - 签名 View 不适用于 Android 8 + 设备

java - 使用 TCP/IP 套接字的多线程