-
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class MathQuizGenerator {
public static void main(String[] args) {
int numberOfQuestions = 10; // 设定生成题目的数量
int minNumber = 1; // 题目数字的最小值
int maxNumber = 100; // 题目数字的最大值
Set<String> questions = new HashSet<>(); // 用于存储题目的集合
for (int i = 0; i < numberOfQuestions; i++) {
String question;
do {
// 生成随机数字和运算符
int num1 = getRandomNumber(minNumber, maxNumber);
int num2 = getRandomNumber(minNumber, maxNumber);
char operator = getRandomOperator();
// 构建题目字符串
question = num1 + " " + operator + " " + num2;
} while (questions.contains(question)); // 检测是否重复
// 添加题目到集合
questions.add(question);
// 输出题目
System.out.println("Question " + (i + 1) + ": " + question);
}
}
// 生成指定范围内的随机整数
private static int getRandomNumber(int min, int max) {
Random random = new Random();
return random.nextInt((max - min) + 1) + min;
}
// 随机生成加减乘除运算符
private static char getRandomOperator() {
char[] operators = {'+', '-', '*', '/'};
Random random = new Random();
return operators[random.nextInt(operators.length)];
}
}