public class PrimaryQuestionGenerator extends AbstractQuestionGenerator { private static final String[] OPERATORS = {"+", "-", "×", "÷"}; @Override public String generateQuestion() { int attempts = 0; while (attempts < 100) { attempts++; int operandCount = random.nextInt(4) + 2; // 2~5个操作数 StringBuilder question = new StringBuilder(); // 生成操作数和运算符 for (int i = 0; i < operandCount; i++) { question.append(generateNumber()); if (i < operandCount - 1) { question.append(" ").append(generateOperator(OPERATORS)).append(" "); } } // 随机添加括号 String result = addReasonableParentheses(question.toString(), operandCount); result = ensureBalancedParentheses(result); // 确保括号平衡 if (isQuestionUnique(result) && isValidParentheses(result) && hasOperator(result)) { addToGeneratedQuestions(result); String regex = "(?= start * 2 + 2) { newExpr.append(" )"); } if (i < parts.length - 1) { newExpr.append(" "); } } return newExpr.toString().trim(); } }