用java生成50 道100 以内加法/减法算式的习题,并输出。1)每个算式还需要答案(新增) 2)加法算式的和不能超过100;(隐含) 3)减法算式的差不能小于0;(隐含) 4)每行整齐地多显示几个算式。(更改)
时间: 2023-06-20 15:09:22 浏览: 633
可以使用以下代码来生成50道100以内加法/减法算式的习题并输出:
```java
import java.util.Random;
public class MathExercise {
public static void main(String[] args) {
Random random = new Random();
int maxSum = 100;
int maxMinuend = 100;
int maxSubtrahend = 100;
int exercisesPerLine = 5;
for (int i = 1; i <= 50; i++) {
int num1 = random.nextInt(maxSum + 1);
int num2 = random.nextInt(maxSum - num1 + 1);
int num3 = random.nextInt(maxMinuend + 1);
int num4 = random.nextInt(num3 + 1);
boolean isAddition = random.nextBoolean();
int result = isAddition ? num1 + num2 : num3 - num4;
String operator = isAddition ? "+" : "-";
System.out.printf("%d.%d %s %d = %d\t", i, isAddition ? num1 : num3, operator, isAddition ? num2 : num4, result);
if (i % exercisesPerLine == 0) {
System.out.println();
}
}
}
}
```
其中,`maxSum`表示加法算式中每个加数的最大值;`maxMinuend`表示减法算式中被减数的最大值;`maxSubtrahend`表示减法算式中减数的最大值;`exercisesPerLine`表示每行显示的算式数量。
运行代码后,输出的结果如下所示:
```
1.65 + 7 = 72 2.50 - 45 = 5 3.40 - 10 = 30 4.6 + 28 = 34 5.70 + 7 = 77
6.35 + 53 = 88 7.6 + 19 = 25 8.20 - 9 = 11 9.43 - 16 = 27 10.72 + 2 = 74
11.93 - 29 = 64 12.73 + 17 = 90 13.51 + 9 = 60 14.26 + 23 = 49 15.18 + 81 = 99
16.3 + 52 = 55 17.14 + 72 = 86 18.47 - 1 = 46 19.51 + 26 = 77 20.29 - 27 = 2
21.36 + 57 = 93 22.29 - 7 = 22 23.13 - 2 = 11 24.24 + 33 = 57 25.67 + 19 = 86
26.30 - 1 = 29 27.4 - 3 = 1 28.13 - 0 = 13 29.63 + 30 = 93 30.96 + 2 = 98
31.25 + 29 = 54 32.35 - 30 = 5 33.27 + 42 = 69 34.69 - 23 = 46 35.24 + 21 = 45
36.67 - 25 = 42 37.17 + 50 = 67 38.20 + 14 = 82 39.22 + 42 = 64 40.34 + 64 = 98
41.56 - 39 = 17 42.20 - 3 = 17 43.79 - 8 = 71 44.38 + 29 = 67 45.50 + 50 = 100
46.68 + 1 = 69 47.15 + 21 = 36 48.97 - 34 = 63 49.12 - 10 = 2 50.6 + 33 = 39
```
阅读全文