练习题

练习题重复加法

改写随机减法问题,使它产生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);

	}

}

练习题

上一篇:leetcode119_2-12每日题:杨辉三角


下一篇:一种基于抽取式的中文机器阅读理解数据集CMRC2018