You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
math-learing/src/main/java/com/personalproject/model/QuizQuestion.java

74 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.personalproject.model;
import java.util.List;
/**
* 表示一个带有多项选择的测验题目.
*/
public final class QuizQuestion {
private final String questionText;
private final List<String> options;
private final int correctAnswerIndex;
/**
* 创建新的测验题目.
*
* @param questionText 题目文本
* @param options 答案选项列表
* @param correctAnswerIndex 正确答案在选项列表中的索引
*/
public QuizQuestion(String questionText, List<String> options, int correctAnswerIndex) {
if (questionText == null || questionText.trim().isEmpty()) {
throw new IllegalArgumentException("Question text cannot be null or empty");
}
if (options == null || options.size() < 2) {
throw new IllegalArgumentException("Options must contain at least 2 choices");
}
if (correctAnswerIndex < 0 || correctAnswerIndex >= options.size()) {
throw new IllegalArgumentException("Correct answer index out of bounds");
}
this.questionText = questionText;
this.options = List.copyOf(options); // 不可变副本
this.correctAnswerIndex = correctAnswerIndex;
}
/**
* 获取题目文本.
*
* @return 题目文本
*/
public String getQuestionText() {
return questionText;
}
/**
* 获取答案选项列表.
*
* @return 不可修改的答案选项列表
*/
public List<String> getOptions() {
return options;
}
/**
* 获取正确答案在选项列表中的索引.
*
* @return 正确答案的索引
*/
public int getCorrectAnswerIndex() {
return correctAnswerIndex;
}
/**
* 检查给定的答案索引是否正确.
*
* @param answerIndex 用户答案的索引
* @return 若答案正确则返回 true否则返回 false
*/
public boolean isAnswerCorrect(int answerIndex) {
return answerIndex == correctAnswerIndex;
}
}