练习题重复加法
改写随机减法问题,使它产生10个随机加法问题,加数是两个1到15之间的整数。显示正确答案的个数和完成测试的时间。
import java.util.Scanner;
public class RepeatAddition {
public static void main(String[] args) {
final int NUMBER_OF_QUESTIONS = 10;
int correctCount = 0;
int count = 0;
long startTime = System.currentTimeMillis();
String output = " ";
Scanner input = new Scanner(System.in);
while (count < NUMBER_OF_QUESTIONS) {
int number1 = (int)(Math.random() * 15);
int number2 = (int)(Math.random() * 15);
System.out.print("What is " + number1 + "+" + number2 + "?");
int answer = input.nextInt();
if (number1 +number2 == answer) {
System.out.println("You are correct!");
correctCount++;
}
else
System.out.println("Your anwer is wrong.\n" + number1 + "+"+number2 + " should be " + (number1 + number2));
count++;
output += "\n"+number1+"-"+number2+"="+answer+((number1 + number2 == answer) ? "correct":"wrong");
}
long endTime = System.currentTimeMillis();
long testTime = endTime - startTime;
System.out.println("Correct count is" + correctCount + "\nTest time is "+testTime / 1000 +"seconds\n"+ output);
}
}