import java.util.Random; public class Main { public static void main(String[] args) { Random rand = new Random(); for (int i = 0; i < 20; i++) { int num1 = rand.nextInt(100) + 1; int num2 = rand.nextInt(100) + 1; int operator = rand.nextInt(2); // 0代表加号,1代表减号 char operatorChar = operator == 0 ? '+' : '-'; System.out.println(num1 + " " + operatorChar + " " + num2 + " = "); } } }
改进目标:
- 避免重复题目
- 可控制题目数量
- 增加乘除法