随即生成10题加法运算题,计算得分
import java.util.Scanner;
import java.util.Random;
public class For02{
public static voic main(String[] args){
System.out.println("欢迎参加十道数学题目的测试(输入0结束)请准备,GO!GO!GO!:");
Random ran = new Random();
Scanner s = new Scanner(System.in);
int num1 = 0;
int num2 = 0;
int answer = 0;
int result = 0;
int score = 0;
for(int i =1;i<=10;i++){
num1 = ran.nextInt(100) + 1;
num2 = ran.nextInt(100) + 1;
System.out.println(i + ")." + num1 + " + " + num2 + " = ?");
//得到正确答案
answer = num1 + num2;
//得到用户从键盘读入的结果
result = s.nextInt();
if(result == 0){
break;
}else if(result == answer){
System.out.println("答对了!你真棒!");
score += 10;
}else{
System.out.println("答错了!你真笨!");
}
}
s.close();
if(result == 0){
System.out.println("太遗憾了,下次再来玩吧!");
}else{
System.out.println("本次加法测试已经结束,您的最终得分为:" + score);
}
}
}