修复题目生成逻辑:确保操作数数量符合要求

- 小学题目:操作数至少为2个,避免出现单个操作数题目
- 初中题目:操作数可以为1个(涉及平方、开根号运算)
- 高中题目:操作数可以为1个(涉及三角函数运算)
- 添加题目验证逻辑,确保生成的题目符合难度要求
- 优化题目生成算法,增加题目多样性
main
imok 5 days ago
parent 6cc655468a
commit 37e9a91704

@ -0,0 +1,39 @@
1. (9)² =
2. √99 =
3. (5)² =
4. √86 + (39) + 56 / 86 =
5. √44 + ((21) * 49) / 19 / 15 / 6 =
6. (4)² =
7. (7)² =
8. (5)² + (36 - 53 + 24) =
9. √27 =
10. (2)² =
11. (6)² + ((30) / 29 / 87) * 6 + 49 =
12. √54 =
13. √73 =
14. (5)² + 14 * 89 / 65 / 6 - 16 =
15. √55 =
16. √74 + (((16) - 29) * 26 - 34) =
17. √11 =
18. √41 + ((71) / 57 - 16 * 8) + 90 =
19. (6)² =
20. √63 =

@ -0,0 +1,39 @@
1. sin(67°) + ((91) - 77) + 2 =
2. cos(40°) + (88) / 44 + 69 + 17 + 93 =
3. sin(20°) + 84 + 3 =
4. tan(16°) + 31 * 7 + 87 + 64 =
5. tan(18°) + (((24 - 34) - 27 * 58) * 67) =
6. cos(26°) + ((77 - 15 * 79) / 14 - 19) =
7. sin(24°) + 71 =
8. tan(14°) + 28 + 83 =
9. sin(7°) + (84) / 54 / 48 / 64 + 39 =
10. cos(21°) + 66 =
11. sin(33°) + 53 =
12. cos(37°) + 93 + 83 / 50 / 6 =
13. sin(38°) + (86 / 53 / 19 * 31) =
14. sin(48°) + 8 / 18 =
15. cos(83°) + 53 + 99 =
16. cos(73°) + 28 / 36 =
17. cos(10°) + 13 - 13 =
18. cos(83°) + 2 =
19. tan(35°) + (83) * 31 / 11 - 93 =
20. tan(26°) + (((53 + 63) + 74) + 22 / 58) =

@ -8,27 +8,28 @@ public class MathQuestion implements QuestionInterface {
private String questionText;
private DifficultyLevel difficulty;
private Random random;
private boolean isValid;
public MathQuestion(DifficultyLevel difficulty) {
this.difficulty = difficulty;
this.random = new Random();
this.questionText = generateQuestion();
this.isValid = validateQuestion();
}
@Override
public String generateQuestion() {
int operandCount = random.nextInt(5) + 1;
StringBuilder question = new StringBuilder();
switch (difficulty) {
case PRIMARY:
question.append(generatePrimaryQuestion(operandCount));
question.append(generatePrimaryQuestion());
break;
case JUNIOR:
question.append(generateJuniorQuestion(operandCount));
question.append(generateJuniorQuestion());
break;
case SENIOR:
question.append(generateSeniorQuestion(operandCount));
question.append(generateSeniorQuestion());
break;
}
@ -38,7 +39,33 @@ public class MathQuestion implements QuestionInterface {
@Override
public boolean isValid() {
return questionText != null && !questionText.trim().isEmpty();
return isValid;
}
private boolean validateQuestion() {
if (questionText == null || questionText.trim().isEmpty()) {
return false;
}
// 小学题目必须至少有2个操作数
if (difficulty == DifficultyLevel.PRIMARY) {
String expr = questionText.replace(" = ", "");
// 统计操作符数量来判断操作数数量
int operatorCount = countOperators(expr);
return operatorCount >= 1; // 1个操作符表示2个操作数
}
return true;
}
private int countOperators(String expression) {
int count = 0;
for (char c : expression.toCharArray()) {
if (c == '+' || c == '-' || c == '*' || c == '/') {
count++;
}
}
return count;
}
@Override
@ -51,7 +78,9 @@ public class MathQuestion implements QuestionInterface {
return difficulty;
}
private String generatePrimaryQuestion(int operandCount) {
private String generatePrimaryQuestion() {
// 小学题目确保至少有2个操作数2-5个
int operandCount = random.nextInt(4) + 2; // 2-5个操作数
String[] operators = {"+", "-", "*", "/"};
StringBuilder question = new StringBuilder();
@ -61,6 +90,7 @@ public class MathQuestion implements QuestionInterface {
}
question.append(random.nextInt(100) + 1);
// 随机添加括号(小学难度)
if (operandCount > 2 && random.nextDouble() < 0.3) {
question.insert(0, "(").append(")");
}
@ -68,19 +98,51 @@ public class MathQuestion implements QuestionInterface {
return question.toString();
}
private String generateJuniorQuestion(int operandCount) {
String question = generatePrimaryQuestion(operandCount);
if (random.nextBoolean()) {
return "√" + (random.nextInt(100) + 1) + " + " + question;
} else {
return "(" + (random.nextInt(10) + 1) + ")² + " + question;
private String generateJuniorQuestion() {
// 初中题目可以有1个操作数涉及平方、开根号
int choice = random.nextInt(3);
switch (choice) {
case 0:
// 单个操作数:开根号
return "√" + (random.nextInt(100) + 1);
case 1:
// 单个操作数:平方
return "(" + (random.nextInt(10) + 1) + ")²";
case 2:
// 多个操作数:基础运算 + 平方/开根号
String baseQuestion = generatePrimaryQuestion();
if (random.nextBoolean()) {
return "√" + (random.nextInt(100) + 1) + " + " + baseQuestion;
} else {
return "(" + (random.nextInt(10) + 1) + ")² + " + baseQuestion;
}
default:
return generatePrimaryQuestion();
}
}
private String generateSeniorQuestion(int operandCount) {
String question = generatePrimaryQuestion(operandCount);
private String generateSeniorQuestion() {
// 高中题目可以有1个操作数涉及三角函数
int choice = random.nextInt(3);
String[] trigFunctions = {"sin", "cos", "tan"};
return trigFunctions[random.nextInt(trigFunctions.length)] +
"(" + (random.nextInt(90) + 1) + "°) + " + question;
switch (choice) {
case 0:
// 单个操作数:三角函数
return trigFunctions[random.nextInt(trigFunctions.length)] +
"(" + (random.nextInt(90) + 1) + "°)";
case 1:
// 三角函数 + 基础运算
String baseQuestion = generatePrimaryQuestion();
return trigFunctions[random.nextInt(trigFunctions.length)] +
"(" + (random.nextInt(90) + 1) + "°) + " + baseQuestion;
case 2:
// 复杂组合:三角函数 + 平方/开根号 + 基础运算
String complexBase = generateJuniorQuestion();
return trigFunctions[random.nextInt(trigFunctions.length)] +
"(" + (random.nextInt(90) + 1) + "°) + " + complexBase;
default:
return generatePrimaryQuestion();
}
}
}

@ -31,7 +31,8 @@ public class QuestionGenerator {
return null;
}
} while (existingQuestions.contains(question.getQuestionText()) ||
newQuestions.contains(question.getQuestionText()));
newQuestions.contains(question.getQuestionText()) ||
!question.isValid());
newQuestions.add(question.getQuestionText());
questions[i] = (i + 1) + ". " + question.getQuestionText();

Loading…
Cancel
Save