用Java语言编译运算数、运算结果值的范围都在[0.100]的加法、减法、加减混合50道题,并分别输出符合要求的50道加法、减法、加减混合题个2套。
时间: 2023-07-10 11:07:36 浏览: 45
以下是用Java语言编写的生成50道加法、减法、加减混合的代码,每种类型各生成两套:
```java
import java.util.Random;
public class MathQuizGenerator {
private static final int NUM_QUESTIONS = 50;
private static final int MAX_VALUE = 100;
private static final double MAX_RESULT = 100.0;
private static final Random random = new Random();
public static void main(String[] args) {
for (int i = 1; i <= 2; i++) {
System.out.println("Set " + i + " - Addition:");
generateAdditionQuestions();
System.out.println("\nSet " + i + " - Subtraction:");
generateSubtractionQuestions();
System.out.println("\nSet " + i + " - Mixed Operations:");
generateMixedQuestions();
System.out.println();
}
}
private static void generateAdditionQuestions() {
for (int i = 1; i <= NUM_QUESTIONS; i++) {
int a = random.nextInt(MAX_VALUE + 1);
int b = random.nextInt(MAX_VALUE + 1 - a);
int result = a + b;
if (result <= MAX_RESULT) {
System.out.println(i + ") " + a + " + " + b + " = ");
} else {
i--;
}
}
}
private static void generateSubtractionQuestions() {
for (int i = 1; i <= NUM_QUESTIONS; i++) {
int a = random.nextInt(MAX_VALUE + 1);
int b = random.nextInt(a + 1);
int result = a - b;
if (result <= MAX_RESULT) {
System.out.println(i + ") " + a + " - " + b + " = ");
} else {
i--;
}
}
}
private static void generateMixedQuestions() {
for (int i = 1; i <= NUM_QUESTIONS; i++) {
int a = random.nextInt(MAX_VALUE + 1);
int b = random.nextInt(MAX_VALUE + 1);
int c = random.nextInt(2);
int result;
String operator;
if (c == 0) {
result = a + b;
operator = " + ";
} else {
if (a < b) {
int temp = a;
a = b;
b = temp;
}
result = a - b;
operator = " - ";
}
if (result <= MAX_RESULT) {
System.out.println(i + ") " + a + operator + b + " = ");
} else {
i--;
}
}
}
}
```
运行以上代码将会输出两套符合要求的50道加法、减法、加减混合题。