打印答题时间、正确题目数

/*
What's 25-32 ? 556
What's 34-13 ? 90
What's 69-91 ? -22
What's 66-86 ? 20
What's 8-51 ? -43
Correct count is: 2
Test time is: 0 hour 8 minute 24 seconds
25-32=556   wrong.
34-13=90    wrong.
69-91=-22   Right.
66-86=20    wrong.
8-51=-43    Right.
*/
import java.util.Scanner;

public class SubtrationQuizloop {
  public static void main (String[] args) {
    long startSeconds = System.currentTimeMillis() / 1000;
    Scanner input = new Scanner(System.in);
    int correctCount = 0, answer;  //Count the number of the correct questions
    final int NUMBER_OF_QUESTIONS = 5;
    final int PER_SECONDS_MINUTE = 60;
    String output = ""; // output string is initially empty
    String isRight = "";

    for (int count = 0; count < NUMBER_OF_QUESTIONS; count++) {
      int number1 = (int)(Math.random() * 100);
      int number2 = (int)(Math.random() * 100);

      System.out.print("What's " + number1 + "-" + number2 + " ? ");
      answer = input.nextInt();
      if(number1 - number2 == answer) {
          correctCount++;
          isRight = "Right.";
      }
      else
        isRight = "wrong.";
      output += number1 + "-" + number2 + "=" + answer + "\t" + isRight + "\n";
    }

    long endSeconds = System.currentTimeMillis() / 1000;
    int totalSeconds = (int)(endSeconds - startSeconds);
    int seconds = totalSeconds % PER_SECONDS_MINUTE;
    totalSeconds /= PER_SECONDS_MINUTE;
    int minute = totalSeconds % PER_SECONDS_MINUTE;
    int hour = totalSeconds / PER_SECONDS_MINUTE;

    System.out.println("Correct count is: " + correctCount);
    System.out.println("Test time is: " + hour + " hour " + minute + " minute "
                        + seconds + " seconds");
    System.out.println(output);
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值