完成布置的Java小练习。
题目:一家软件公司程序员二柱的小孩上了小学二年级,老师让家长每天出30道四则运算题目给小学生做。
代码如下:
import java.util.Random;
public class TestDouble {
public static void main(String[] args) {
Random random = new Random();
for (int i = 1; i <= 30; i++) {
int num1 = random.nextInt(100) + 1; // 生成1到100之间的随机数
int num2 = random.nextInt(100) + 1;
int operation = random.nextInt(4); // 0:加法, 1:减法, 2:乘法, 3:除法
// 除法需要特别处理,确保可以整除
if (operation == 3) {
int temp = num1;
num1 = num1 * num2;
num2 = temp;
}
// 输出题目
System.out.print("题目" + i + ": ");
switch (operation) {
case 0:
System.out.println(num1 + " + " + num2);
break;
case 1:
System.out.println(num1 + " - " + num2);
break;
case 2:
System.out.println(num1 + " * " + num2);
break;
case 3:
System.out.println(num1 + " / " + num2);
break;
}
}
}
}
输出的结果是:
题目1: 32 * 42
题目2: 20 - 30
题目3: 3710 / 70
题目4: 90 - 68
题目5: 56 + 59
题目6: 8 - 17
题目7: 86 - 18
题目8: 94 + 78
题目9: 24 * 89
题目10: 96 + 29
题目11: 17 - 33
题目12: 228 / 38
题目13: 55 - 50
题目14: 800 / 50
题目15: 4418 / 47
题目16: 88 + 9
题目17: 70 - 91
题目18: 1311 / 57
题目19: 5 + 8
题目20: 93 + 58
题目21: 15 - 72
题目22: 66 * 23
题目23: 83 + 15
题目24: 91 * 86
题目25: 38 * 31
题目26: 92 - 1
题目27: 19 + 55
题目28: 98 * 9
题目29: 30 * 65
题目30: 58 + 78