fix(question): 修复题目答案验证逻辑

dev
zhaokai 2026-03-02 16:48:38 +08:00
parent 89c4693002
commit ef680987c8
1 changed files with 27 additions and 0 deletions

View File

@ -145,6 +145,10 @@ public class QuestionE extends BaseE {
errList.add("单选题第" + (i + 2) + "行分值不能为空"); errList.add("单选题第" + (i + 2) + "行分值不能为空");
} }
} }
//单选题答案只能是ABCD四个中的一个不能多个
if (!entity.getAnswer().matches("[A-D]") ||entity.getAnswer().length()>1) {
errList.add("单选题第" + (i + 2) + "行答案错误");
}
} }
} }
if (CollUtil.isNotEmpty(videoMultiselectList)) { if (CollUtil.isNotEmpty(videoMultiselectList)) {
@ -180,6 +184,12 @@ public class QuestionE extends BaseE {
errList.add("多选题第" + (i + 2) + "行分值不能为空"); errList.add("多选题第" + (i + 2) + "行分值不能为空");
} }
} }
String answer = entity.getAnswer().toUpperCase().trim();
//多选题答案只能是选题答案只能是ABCD四个中的值可以多个
if (answer.matches("^[A-D]{2,4}$") && ! hasDuplicateChars(answer)) {
errList.add("多选题第" + (i + 2) + "行答案错误");
}
} }
} }
if (CollUtil.isNotEmpty(videoJudgeList)) { if (CollUtil.isNotEmpty(videoJudgeList)) {
@ -208,6 +218,10 @@ public class QuestionE extends BaseE {
} }
} }
} }
//判断题答案只能是AB中的一个
if (!entity.getAnswer().matches("[A-B]") ||entity.getAnswer().length()>1) {
errList.add("单选题第" + (i + 2) + "行答案错误");
}
} }
} }
@ -289,5 +303,18 @@ public class QuestionE extends BaseE {
return questionEList; return questionEList;
} }
public static boolean hasDuplicateChars(String str) {
if (str == null || str.length() <= 1) {
return false;
}
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
if (str.charAt(i) == str.charAt(j)) {
return true;
}
}
}
return false;
}
} }