java用20以内加减法
时间: 2024-05-02 08:21:42 浏览: 125
以下是Java代码,使用20以内的加减法:
```java
import java.util.Random;
public class AdditionSubtractionWithin20 {
public static void main(String[] args) {
Random random = new Random();
int num1, num2, result;
char operator;
for (int i = 0; i < 10; i++) {
num1 = random.nextInt(20) + 1;
num2 = random.nextInt(20) + 1;
operator = random.nextBoolean() ? '+' : '-';
if (operator == '+') {
result = num1 + num2;
System.out.println(num1 + " + " + num2 + " = " + result);
} else {
result = num1 - num2;
System.out.println(num1 + " - " + num2 + " = " + result);
}
}
}
}
```
输出结果:
```
1 + 20 = 21
19 - 17 = 2
11 + 2 = 13
13 + 1 = 14
17 - 1 = 16
3 - 2 = 1
20 - 19 = 1
1 + 9 = 10
11 - 5 = 6
1 - 1 = 0
```
阅读全文