首页 > 试题广场 >

( 重复加法)程序清单 54 产生了 5 个随机减法问题。改

[问答题]
( 重复加法)程序清单 54 产生了 5 个随机减法问题。改写该程序,使它产生 10 个随机加法问题, 加数是两个1到 15 之间的整数。显示正确答案的个数和测验时间。 
public class Test {
	public static void main(String[] args){
	

		//程序说明:程序产生10个随机加法,显示正确的个数和测验时间
		//产生加法
		long startTime = System.currentTimeMillis();
		int s1 = 0,s2 = 0,a = 0,t = 0;
		int i= 0;
		Scanner input = new Scanner(System.in);
		while(i < 10){
			//产生两个随机数
			s1 = (int)(Math.random() * 15 + 1);
			s2 = ((int)Math.random() * 15 + 1);
			System.out.print("Whta is " + s1 + "+" + s2 + "? ");
			a = input.nextInt();
			if(s1 + s2 == a)
			{
				System.out.println("You are correct!");
				t++;
			}
				
			else
				{
				System.out.println("You are wrong!");
				System.out.println(s1 + " + " + s2 + " should be " + (s1 + s2));
				}
			i++;
		}
		//显示正确的个数和时间
		System.out.println("correct count is " + t);
			//显示时间
		long endTime = System.currentTimeMillis();
		long testTime = endTime - startTime;
		
		System.out.println("Test time is " + testTime / 1000 + " seconds");
    }
}

发表于 2020-02-25 10:32:09 回复(0)