用java随机生成10以内的四则混合运算题并存放到字符串组中
时间: 2024-01-25 12:05:03 浏览: 77
可以使用以下代码来随机生成10以内的四则混合运算题并存放到字符串数组中:
```java
import java.util.Random;
public class Main {
public static void main(String[] args) {
Random random = new Random();
String[] questions = new String[10];
for (int i = 0; i < questions.length; i++) {
int a = random.nextInt(10) + 1;
int b = random.nextInt(10) + 1;
int op = random.nextInt(4);
String operator = "";
int result = 0;
switch (op) {
case 0:
operator = "+";
result = a + b;
break;
case 1:
operator = "-";
result = a - b;
break;
case 2:
operator = "*";
result = a * b;
break;
case 3:
operator = "/";
result = a / b;
break;
}
questions[i] = a + " " + operator + " " + b + " = ?";
}
for (String question : questions) {
System.out.println(question);
}
}
}
```
运行结果类似于:
```
2 - 3 = ?
10 / 4 = ?
8 * 10 = ?
9 / 10 = ?
6 * 9 = ?
5 - 2 = ?
4 * 9 = ?
4 + 3 = ?
9 + 9 = ?
10 - 7 = ?
```
阅读全文