用java语言写一个生成100以内加减法算式并且结果也是100以内
时间: 2024-05-08 20:17:31 浏览: 298
的程序,每次运行程序生成10道算式。
```java
import java.util.Random;
public class MathGenerator {
public static void main(String[] args) {
Random random = new Random();
int maxNum = 100;
int numOfQuestions = 10;
for (int i = 0; i < numOfQuestions; i++) {
int num1 = random.nextInt(maxNum) + 1;
int num2 = random.nextInt(maxNum) + 1;
int operator = random.nextInt(2); // 0: addition, 1: subtraction
int result;
String operatorSymbol;
if (operator == 0) {
result = num1 + num2;
operatorSymbol = "+";
} else {
result = num1 - num2;
operatorSymbol = "-";
}
if (result >= 0 && result <= maxNum) {
System.out.println(num1 + " " + operatorSymbol + " " + num2 + " = " + result);
} else {
i--; // regenerate question
}
}
}
}
```
阅读全文