You need to enable JavaScript to run this app. 如何用Java实现测验每道题的作答时间与耗时记录功能
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java实现测验每道题的作答时间与耗时记录功能

Java选择题测验单题时间统计解决方案

核心修改思路

要实现单题作答时刻、单题耗时统计,只需为每道题单独记录时间节点即可,无需改动原有业务逻辑:

  • 每道题展示完成后、等待用户输入答案前,记录当前时间作为单题作答开始时间
  • 用户提交答案后立即计算单题耗时,同时记录当前时间作为作答提交时刻
  • 为每道题新增变量存储对应的作答时刻、耗时数据,最终输出时补入表格即可

修改后完整代码

package timer;
import java.util.Date;
import java.util.Scanner;
import java.text.*;
import java.util.concurrent.TimeUnit;

public class num4 {
public static void main (String [] args) throws InterruptedException{
    
   Scanner scan=new Scanner(System.in); 
    Date date=new Date();
    SimpleDateFormat ft = 
  new SimpleDateFormat("MMMM dd,yyyy '@'hh:mm a ");
   String one,two,three,four,five;
   String o,t,th,f,fi;
   int variable=0,wrong=0,seconds,minutes;
   // 新增单题时间存储变量
   String q1Time,q2Time,q3Time,q4Time,q5Time;
   int q1Cost,q2Cost,q3Cost,q4Cost,q5Cost;
   final long startTime = System.nanoTime();
   System.out.println("Welcome to the rawr game!\n");
   Thread.sleep(1000);

   System.out.println("Question #1");
   System.out.println("What can one catch that is not thrown?");
   System.out.println("[A] rain");
   System.out.println("[B] a cold");
   System.out.println("[C] a secret");
   System.out.println("[D] snow");
   // 记录本题开始作答时间
   long q1Start = System.nanoTime();
    System.out.print("\nYour answer: ");
    one=scan.next();
    // 计算本题耗时、存储作答时间
    long q1End = System.nanoTime();
    q1Cost = (int) ((q1End - q1Start)/1e9);
    q1Time = ft.format(new Date());
    if(one.equals("B")){
        o="Correct";
        variable=variable+1;
    }else{
         o="Wrong";
         wrong=wrong+1;
    }
    
   System.out.println("\nQuestion #2");
   System.out.println("Pambansang Editor: ");
   System.out.println("[A] Netflix");
   System.out.println("[B] Youtube");
   System.out.println("[C] Facebook");
   System.out.println("[D] Instagram");
   long q2Start = System.nanoTime();
   System.out.print("\nYour answer: ");
   two=scan.next();
   long q2End = System.nanoTime();
   q2Cost = (int) ((q2End - q2Start)/1e9);
   q2Time = ft.format(new Date());
   if(two.equals("D")){
        t="Correct";
        variable=variable+1;
    }else{
         t="Wrong";
          wrong=wrong+1;
    }
   
   System.out.println("\nQuestion #3");
   System.out.println("Indian's Population in 2020");
   System.out.println("[A] 1.333 billion");
   System.out.println("[B] 1.300 billion");
   System.out.println("[C] 1.333 million");
   System.out.println("[D] 1.336 billion");
   long q3Start = System.nanoTime();
   System.out.print("\nYour answer: ");
   three=scan.next();
   long q3End = System.nanoTime();
   q3Cost = (int) ((q3End - q3Start)/1e9);
   q3Time = ft.format(new Date());
   if(three.equals("D")){
        th="Correct";
        variable=variable+1;
    }else{
         th="Wrong";
          wrong=wrong+1;
    }
   
   System.out.println("\nQuestion #4");
   System.out.println("Man's Bestfriend?");
   System.out.println("[A] dogs");
   System.out.println("[B] cat");
   System.out.println("[C] hamster");
   System.out.println("[D] none");
   long q4Start = System.nanoTime();
   System.out.print("\nYour answer: ");
   four=scan.next();
   long q4End = System.nanoTime();
   q4Cost = (int) ((q4End - q4Start)/1e9);
   q4Time = ft.format(new Date());
   if(four.equals("A")){
        f="Correct";
        variable=variable+1;
    }else{
         f="Wrong";
          wrong=wrong+1;
    }
   
   System.out.println("\nQuestion #5");
   System.out.println("Who steal the pot of soup in Episode 100 of Mark Angel Comedy? ");
   System.out.println("[A] Emmanuella");
   System.out.println("[B] The Iphone Girl");
   System.out.println("[C] Denilson");
   System.out.println("[D] Kbrown");
   long q5Start = System.nanoTime();
   System.out.print("\nYour answer: ");
   five=scan.next();
   long q5End = System.nanoTime();
   q5Cost = (int) ((q5End - q5Start)/1e9);
   q5Time = ft.format(new Date());
   if(five.equals("C")){
        fi="Correct";
        variable=variable+1;
    }else{
         fi="Wrong";
          wrong=wrong+1;
    }
   
   

  System.out.println("=====");
  System.out.println("Rawr quiz on " + ft.format(date));
  System.out.println("=====");
  
  System.out.println("Question #\tYour Answer\tRemarks\tTime\t\t\tDuration");
  System.out.println("1\t\t"+one+"\t\t"+o+"\t"+q1Time+"\t"+q1Cost+"秒");
  System.out.println("2\t\t"+two+"\t\t"+t+"\t"+q2Time+"\t"+q2Cost+"秒");
  System.out.println("3\t\t"+three+"\t\t"+th+"\t"+q3Time+"\t"+q3Cost+"秒");
  System.out.println("4\t\t"+four+"\t\t"+f+"\t"+q4Time+"\t"+q4Cost+"秒");
  System.out.println("5\t\t"+five+"\t\t"+fi+"\t"+q5Time+"\t"+q5Cost+"秒");
  
  System.out.println("Number of Correct Answers: "+variable+"\t\tNumber of Wrong Answers: "+wrong);

  final long duration = System.nanoTime()- startTime;
  seconds =(int) (duration/1000000000);
  minutes= (int)  (seconds/60);
  if (minutes<=1){
      System.out.println("Total Duration: "+minutes+" minute");
  }else{
      System.out.println("Total Duration: "+minutes+" minutes");
  }
  

 }
 }

优化建议

如果后续题目数量会增加,可以把题目、正确答案、用户作答数据、时间数据都封装到对象中,用数组/集合存储,避免重复编写相似代码。

内容的提问来源于stack exchange,提问作者Marie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 17:54:00