以下是简单的减法运算,可以自行修改。
package test;
import java.util.Scanner;
public class SubtracitonQuizLoop {
public static void main(String[] args){
//定义初始量
Scanner input = new Scanner(System.in);
long startTime = System.currentTimeMillis();
//主体
System.out.print("您要练习几道题呢? ");
int numbers = input.nextInt();
while(numbers > 0){
int a = (int)(Math.random()*100);
int b = (int)(Math.random()*100);
//排序,大的放前面
if(a < b){
int temp;
temp = a;
a = b;
b = temp;
}
int c = a - b;
System.out.print("What is " + a + "-" + b + "? ");
int answer = input.nextInt();
//判断值是否正确
if(answer == c){
System.out.println("You are correct!");
}
else{
System.out.println("You answer is wrong.");
}
numbers --;
System.out.println();
}
//结算时间并输出
long finishTime = System.currentTimeMillis();
System.out.print("Test time is " + (finishTime - startTime)/1000 + " second.");
}
}