java 随机产生计算题_Java编写一个随机产生小学生四则运算题30道
时间: 2024-01-15 10:03:48 浏览: 71
java实现随机输出300题四则运算
好的,下面是 Java 代码实现:
```java
import java.util.Random;
public class MathQuiz {
public static void main(String[] args) {
Random random = new Random();
int count = 30; // 题目数量
int range = 100; // 随机数范围
String[] operators = {"+", "-", "*", "/"}; // 运算符
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.println((i + 1) + ". " + a + " " + operator + " " + b + " = " + result);
}
}
}
```
以上代码会生成 30 道小学生四则运算题,每道题目中的数字在 1 到 100 之间随机生成,运算符为加、减、乘、除(概率相等)。
阅读全文