Java简单减法测试并记做题时间
描述
设计5道10以内的随机减法测试题,要求回答完立刻判断对错,如果错误需要显示正确答案,并统计正确的题数和答题和时间,最后显示做题情况。 |
输入
输入所给减法题的计算答案 |
输出
输出随机生成的题目; 如果输入的数与答案一致,输出System.out.println("You are correct!"); 如果输入的数与答案不一致,则输出System.out.println("Your answer is wrong.\n" +n1 + " - " + n2 + " should be " + (n1 - n2)); 答完题后输出答题正确的题数和测试时间; 最后输出做题情况,即题目、输入答案、是否正确,例如:4 - 1 = 2 wrong ,4 - 4 = 0 correct。 |
难度
较难 |
输入示例
5 |
输出示例
What is 9 - 4 = ?
5 You are correct! What is 8 - 0 = ? 8 You are correct! What is 4 - 1 = ? 3 You are correct! What is 6 - 5 = ? 1 You are correct! What is 9 - 8 = ? 2 Your answer is wrong. 9 - 8 should be 1 Correct count is 4 Test time is 8 seconds 9 - 4 = 5 correct
8 - 0 = 8 correct 4 - 1 = 3 correct 6 - 5 = 1 correct 9 - 8 = 2 wrong |
import java.util.Scanner; public class AdditionQuiz { public static void main(String[] args) { final int NUMBER_OF_QUESTIONS = 5; int correctCount = 0; int count = 0; long starTime = System.currentTimeMillis();//重点 String output = ""; Scanner input = new Scanner(System.in); while (count< NUMBER_OF_QUESTIONS) { //int n1=(int)(System.currentTimeMillis() * 10); //int n2=(int)(System.currentTimeMillis() * 10); int n1 = (int) (Math.random() * 10); int n2 = (int) (Math.random() * 10); if (n1 < n2) { int t = n1; n1 = n2; n2 = t; } System.out.println( "What is " + n1 + " - " + n2 + " = ?" ); int answer = input.nextInt(); if (n1 - n2 == answer) { System.out.println("You are correct!"); correctCount++; } else System.out.println("Your answer is wrong.\n" + n1 + " - " + n2 + " should be " + (n1 - n2)); count++; output += "\n" + n1 + " - " + n2 + " = " + answer + ((n1 - n2 == answer) ? " correct" : " wrong"); } long endTime = System.currentTimeMillis(); long testTime = endTime - starTime; System.out.println("Correct count is " + correctCount + "\nTest time is " + testTime / 1000 + " seconds\n" + output); input.close(); } }