将develop分支的内容合并到main分支中 #3

Merged
pijtbanfl merged 1 commits from develop into main 5 months ago

@ -309,6 +309,22 @@ public class MathQuestionGenerator {
* @return
*/
private static boolean handleSwitchCommand(String command) {
// 【人工修复】修复字符串越界风险,添加长度检查
// 修改前command.substring(3) 可能抛出StringIndexOutOfBoundsException
// 修改后:先检查命令长度,确保安全访问
if (command.length() < 3) {
System.out.println();
System.out.println("✗ 请输入小学、初中和高中三个选项中的一个");
System.out.println("按回车键继续...");
System.out.flush();
try {
@SuppressWarnings("unused")
int readResult = System.in.read();
} catch (IOException e) {
// 忽略读取错误
}
return false;
}
String targetGrade = command.substring(3).trim();
if (targetGrade.equals("小学") || targetGrade.equals("初中") || targetGrade.equals("高中")) {
@ -517,8 +533,14 @@ public class MathQuestionGenerator {
String line;
while ((line = reader.readLine()) != null) {
if (line.matches("\\d+\\.\\s+.+")) {
String question = line.substring(line.indexOf(".") + 1).trim();
historyQuestions.add(question);
// 【人工修复】修复字符串越界风险,添加安全检查
// 修改前line.indexOf(".") 可能返回-1导致substring(-1+1)问题
// 修改后先检查indexOf结果确保安全访问
int dotIndex = line.indexOf(".");
if (dotIndex != -1 && dotIndex + 1 < line.length()) {
String question = line.substring(dotIndex + 1).trim();
historyQuestions.add(question);
}
}
}
} catch (IOException e) {

@ -24,13 +24,12 @@ public class PrimaryQuestionGenerator extends AbstractQuestionGenerator {
// 随机添加括号,确保括号内也有运算符
if (i < operands.length - 1 && random.nextBoolean()) {
question.append(" ").append(op).append(" (").append(operands[i]);
if (i + 1 < operands.length) {
op = BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
question.append(" ").append(op).append(" ").append(operands[i + 1]).append(")");
i++; // 【人工修复】修复数组越界风险手动增加i以跳过已处理的操作数
} else {
question.append(")");
}
// 【人工修复】修复逻辑错误,移除多余的边界检查
// 修改前if (i + 1 < operands.length) 是多余的因为外层条件已经确保i < operands.length - 1
// 修改后直接处理i+1位置的操作数因为条件已经保证其存在
op = BASIC_OPERATORS[random.nextInt(BASIC_OPERATORS.length)];
question.append(" ").append(op).append(" ").append(operands[i + 1]).append(")");
i++; // 手动增加i以跳过已处理的操作数
} else {
question.append(" ").append(op).append(" ").append(operands[i]);
}

Loading…
Cancel
Save