9.28.2

YOLO霖發表於2024-09-28

import java.util.Random;
public class FourArithmeticOperations {
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 30; i++) {
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int operator = random.nextInt(4);
if (operator == 0) {
System.out.println(num1 + " + " + num2 + " = ");
} else if (operator == 1) {
if (num1 < num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
System.out.println(num1 + " - " + num2 + " = ");
} else if (operator == 2) {
System.out.println(num1 + " × " + num2 + " = ");
} else {
if (num2 == 0) {
num2 = 1;
}
num1 = num1 * num2;
System.out.println(num1 + " ÷ " + num2 + " = ");
}
}
}
}