java实现加减乘除运算符随机生成十道题并判断对错_java 随机出题四则运算
时间: 2023-12-15 20:07:15 浏览: 95
好的,以下是一个 Java 实现加减乘除运算符随机生成十道题并判断对错的示例代码:
```
import java.util.Random;
import java.util.Scanner;
public class ArithmeticQuiz {
public static void main(String[] args) {
Random random = new Random();
Scanner scanner = new Scanner(System.in);
int score = 0;
for (int i = 1; i <= 10; i++) {
int num1 = random.nextInt(100) + 1;
int num2 = random.nextInt(100) + 1;
int operator = random.nextInt(4);
String operatorStr = "";
int answer = 0;
switch (operator) {
case 0:
operatorStr = "+";
answer = num1 + num2;
break;
case 1:
operatorStr = "-";
answer = num1 - num2;
break;
case 2:
operatorStr = "*";
answer = num1 * num2;
break;
case 3:
operatorStr = "/";
answer = num1 / num2;
break;
}
System.out.print(i + ". " + num1 + " " + operatorStr + " " + num2 + " = ");
int userAnswer = scanner.nextInt();
if (userAnswer == answer) {
System.out.println("Correct!");
score++;
} else {
System.out.println("Wrong! The answer is " + answer);
}
}
System.out.println("Your score is " + score + "/10");
}
}
```
这个程序会随机生成十道题目,每道题目包含两个随机数和一个随机的运算符,然后让用户输入答案,最后输出用户的得分。注意,这个程序只是一个示例,还有很多地方可以进行优化,比如让用户选择运算符的范围,让用户选择题目数量等等。
阅读全文