设置sentinel值java代码
导入java。util。随机的 导入java。util。扫描仪
公共类算术游戏{
static int right = 0;
static int wrong = 0;
static int total = 0;
static int per = 0;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Random num = new Random();
Random num1 = new Random();
int r = 0;
while (r != 999) {
total++;
per = (right / total) * 100;
int a = num.nextInt(20);
int b = num1.nextInt(20);
int sum = a + b;
System.out.println(" \n ARITHETIC GAME!");
System.out.println("What is the sum of " + a + " + " + b + " ?");
r = s.nextInt();
if (r == sum) {
System.out.println("Your answer is correct.");
right++;
}
if (r != sum) {
System.out.println("OOPS! Your answer is Wrong");
wrong++;
}
System.out.println("So far you have " + right + " right answers " + wrong + " wrong answers out of total "
+ total + " questions");
}
}
} 我需要设置sentinel值,这样当我输入999时,程序应该终止,响应999不应该计入最终值
# 1 楼答案
将你的循环更改为
然后在这行之后r=s.nextInt()但在检查是否正确之前,请添加: