一、程序要求:
系统随机出10道2个100以内的加减计算题,用户在控制台输入答案,如果答案正确,加10分,否则,不加分.最后计算得分,显示在控制台如果当用户输入200的时候,结束程序,输入得分计算用时,显示在控制台,以秒的形式。
import java.util.Random;
import java.util.Scanner;
public class Calculation {
public static void main(String[] args) {
int result = 0;
int score = 0;
Random rand = new Random();
Scanner scan = new Scanner(System.in);
long beginTime = System.currentTimeMillis();//获取当前系统时间
for(int i=0;i<10;i++){
int num1 = rand.nextInt(100);//0-99随机的数字
int num2 = rand.nextInt(100);//0-99随机的数字
int operator = rand.nextInt(2);//0-1 0:加法,1:减法
if(operator==0){
result = num1 + num2;
System.out.println(num1+"+"+num2+"=?");
}else{
result = num1 - num2;
System.out.println(num1+"-"+num2+"=?");
}
// System.out.println("正确结果:"+result);//作弊
int input = scan.nextInt();//用户输入的结果
if(input==200){
break;
}else if(input==result){
score+=10;
}
// System.out.println("用户得分:"+score);
}
long endTime = System.currentTimeMillis();//结束时间
System.out.println("用户得分:"+score
+",用户用时:"+(endTime-beginTime)/1000.0+"秒");
}
}