java实现计算机随机出题:随机出十道小学四则运算题目,要求操作数为10以内,运算加、减、乘、除,并可以根据用户的答案给出“√”或者“×”提示,最后计算总得分。
时间: 2024-04-01 08:32:06 浏览: 46
好的,下面是 Java 代码实现:
```java
import java.util.Random;
import java.util.Scanner;
public class MathQuiz {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int count = 10; // 题目数量
int range = 10; // 随机数范围
String[] operators = {"+", "-", "*", "/"}; // 运算符
int score = 0; // 总得分
for (int i = 0; i < count; i++) {
int a = random.nextInt(range) + 1;
int b = random.nextInt(range) + 1;
String operator = operators[random.nextInt(4)];
int result = 0;
switch (operator) {
case "+":
result = a + b;
break;
case "-":
result = a - b;
break;
case "*":
result = a * b;
break;
case "/":
result = a / b;
break;
}
System.out.print((i + 1) + ". " + a + " " + operator + " " + b + " = ");
int userAnswer = scanner.nextInt();
if (userAnswer == result) {
System.out.println("√");
score++;
} else {
System.out.println("×");
}
}
System.out.println("总得分:" + score);
}
}
```
以上代码会生成 10 道小学生四则运算题,每道题目中的数字在 1 到 10 之间随机生成,运算符为加、减、乘、除(概率相等)。用户需要输入答案并根据是否正确给出“√”或者“×”提示,最后计算总得分。
阅读全文