【发布时间】:2021-09-05 16:33:32
【问题描述】:
我在记录带有多项选择的测验代码的时间和持续时间时遇到问题。
这是回答完所有问题后最终输出的样本。
问题 # 你的答案备注 持续时间
1 B 错误 10:32 AM 30 秒
2 A 正确 10:33 AM 22 秒 //重复直到问题 #5
这是代码:
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;
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");
System.out.print("\nYour answer: ");
one=scan.next();
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");
System.out.print("\nYour answer: ");
two=scan.next();
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");
System.out.print("\nYour answer: ");
three=scan.next();
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");
System.out.print("\nYour answer: ");
four=scan.next();
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");
System.out.print("\nYour answer: ");
five=scan.next();
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\tDuration");
System.out.println("1\t\t"+one+"\t\t"+o);
System.out.println("2\t\t"+two+"\t\t"+t);
System.out.println("3\t\t"+three+"\t\t"+th); ///as you can see in this part
System.out.println("4\t\t"+four+"\t\t"+f); //// it's not complete
System.out.println("5\t\t"+five+"\t\t"+fi); ///I'm having trouble on how
/// to get the time and duration
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");
}
}
}
【问题讨论】:
-
不要再使用 java.util.Date。使用新的 java.time.* API