import java.util.*;
//实现生成随机三个数,并且把这三个数随机加减法、结果是100以内的,而且生成的题目数量是可控的
public class Four {
//
public static void main(String[] args) {
// write your code here
int operand_1, operand_2, operand_3, operatorOrder1, operatorOrder2;
Random rand = new Random();
char op[] = new char[2];
op[0] = '+';
op[1] = '-';
System.out.print("Please read these questions:\n");
int sum = 0;
//n是生成的随机题目的数目
int n = 5;
for (int i = 0; i <n; ) {
operand_1 = rand.nextInt(100);
operand_2 = rand.nextInt(100);
operand_3 = rand.nextInt(100);
operatorOrder1 = rand.nextInt(2);
operatorOrder2 = rand.nextInt(2);
if (op[operatorOrder1] == '+') {
//结果部分
sum = operand_2 + operand_1;
}
if (op[operatorOrder1] == '-') {
sum = operand_1 - operand_2;
}
//第二个符号的判断
if (op[operatorOrder2] == '+') {
//结果部分
sum = sum + operand_3;
}
if (op[operatorOrder2] == '-') {
sum = sum - operand_3;
}
//生成结果的范围
if (sum >= 0 && sum <= 100) {
//System.out.println("算式"+n+":");
System.out.print(operand_1);
System.out.print(op[operatorOrder1]);
System.out.print(operand_2);
System.out.print(op[operatorOrder2]);
System.out.print(operand_3);
System.out.println("=");
System.out.println("结果是"+sum);
i++;//有效的才加一 无效的不加
} else {
continue;
}
}
}
}
随机生成100以内加减法算式
最新推荐文章于 2025-03-17 17:58:34 发布