package com.company; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int score = 0; for (int i = 1; i <= 10; i++) { int num1 = random.nextInt(100); int num2 = random.nextInt(100); int operator = random.nextInt(2); // 0表示加法,1表示减法 int result; if (operator == 0) { result = num1 + num2; } else { result = num1 - num2; } System.out.print(i + ". " + num1 + (operator == 0 ? " + " : " - ") + num2 + " = "); int answer = scanner.nextInt(); if (answer == result) { System.out.println("恭喜你,答对了!"); score++; } else { System.out.println("很遗憾,答错了。正确答案为:" + result); } } System.out.println("您的得分为:" + score * 10); } }
时间: 2023-05-24 15:07:07 浏览: 202
由于题目代码缺少运算符号,所以无法运行。在result的计算中,应该是num1+num2,而不是num1-num2。
修改后代码如下:
```
package com.company;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int score = 0;
for (int i = 1; i <= 10; i++) {
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int operator = random.nextInt(2);
// 0表示加法,1表示减法
int result;
if (operator == 0) {
result = num1 + num2;
} else {
result = num1 - num2;
}
System.out.print(i + ". " + num1 + (operator == 0 ? " + " : " - ") + num2 + " = ");
int answer = scanner.nextInt();
if (answer == result)
{
System.out.println("恭喜你,答对了!");
score ++;
}
else {
System.out.println("很遗憾,答错了。正确答案为:" + result);
}
}
System.out.println("您的得分为:" + score * 10);
}
}
```
阅读全文