Java 多选题测验

Java quiz with multi choices in answers

我正在尝试开发一个 java 测验,基本思路如下: 一个测验包含 n 个问题,一个问题有 0 到 n 个正确答案,我必须 link 到数据库,所以我的表是:

Table Quiz:

id_quiz Integer Primary key, quiz_name Text

Table Question:

id_question Integer Primary key, question, id_quiz (foreign key)

Table answer:

id_answer Integer primary key, answer TEXT, statut TEXT, id_question (foreign key)

所以我的 java class 是:

public class Quiz {
    int idQuiz;
    String QuizName;

    public Quiz(int id, String quiz) {
        this.idQuiz = id;
        this.QuizName = quiz;
    }

    // getters ...
}

我的问题class:

public class Question {
    int idQuestion;
    String question;
    int idQuiz;

    public Question(int id, String question){
        this.idQuiz = id;
        this.question = question;
    }

    // getters ...
}

答案相同:

public class Answer {
    int idAnswer;
    String answer;
    String statut;
    int idQuestion;

    public Answer(int id, String answer, String statut){
        this.idAnswer = id;
        this.answer = answer;
        this.statut = statut;
    }

    // getters ...
}

JFrame 包含一个 JCombobox 和可用测验列表,选择完成后,一个按钮发送 idQuiz 以获取问题列表选择的测验。 在我的测验 JFrame 中,我创建了一个函数,用下一步按钮填充问题 JLabel,并使用 JRadioButton 到 select.[= 这个问题的多项选择。 26=]

我在获取正确答案列表时遇到了一点问题,用户必须选择所有正确答案,如果有多个正确问题,则不能只选择一个。 有人对此有想法吗? 谢谢。

使用复选框,而不是单选按钮。

单选按钮只允许单选。 Checkbox 是一个控件,您可以在其中选择是否为每个选项选中它。请注意,这也涵盖了 "no checked answers" 选项(虽然可以从没有单选按钮 selected 开始,但一旦用户 selects any单选按钮,他们将无法取消-select).

恐怕你需要 Destructor The Destroyer Of Worlds 的帮助。

他会逐一攻击用户标记为正确的答案,直到决定用户的命运。

首先,你需要这个很重要class:

public class DestructorTheDestroyerOfWorlds
{
    private static List<Answer> correctAnswers;

    public static boolean destroyAnswers(List<Answer> userAnswers)
    {
        boolean success = true;
        for(Answer userAnswer : userAnswers) 
        {
            success &= destroyAnswer(userAnswer);
        }
        return success;
    }

    private static boolean destroyAnswer(Answer userAnswer)
    {
        for (int i = 0; i < correctAnswers.length; i++)
        {
            if (correctAnswers.get(i).idAnswer == userAnswer.idAnswer)
            {
                correctAnswers.remove(i);
                return true;
            }
        }
        return false;
    }

    public static boolean isOutOfAmmo()
    {
        return correctAnswers.isEmpty();
    }

    public static void loadUp(List<Answer> answers)
    {
        correctAnswers = answers;
    }
}

现在我们必须在某个地方实际获取所有信息,所以:

List<Answer> possibleAnswers = readThoseFromTheDatabase();
List<Answer> correctAnswers = doAFilterOn(possibleAnswers);

List<Answer> userAnswers = new List();
if (checkbox1.isSelected()) userAnswers.add(possibleAnswers.get(0));
if (checkbox2.isSelected()) userAnswers.add(possibleAnswers.get(1));
if (checkbox3.isSelected()) userAnswers.add(possibleAnswers.get(2));
if (checkbox4.isSelected()) userAnswers.add(possibleAnswers.get(3));

然后是有趣的部分:

DestructorTheDestroyerOfWorlds.loadUp(correctAnswers);

boolean successfullyDestroyedAllAnswers = DestructorTheDestroyerOfWorlds.destroyAnswers(userAnswers);

if (successfullyDestroyedAllAnswers && DestructorTheDestroyerOfWorlds.isOutOfAmmo())
{
    System.out.println("Oh fateful user, thy answers were right!");
}
else
{
    System.out.println("Oh shameful user, thy answers were wrong!");
}