Java测验答案中有多种选择

时间:2018-05-09 12:05:34

标签: java list jradiobutton

我正在尝试开发一个java测验,这里有一个基本的想法: 一个测验包含n个问题,一个问题有0到n个真实答案,我必须将它链接到数据库,所以我的表是:

Quiz

id_quiz Integer Primary key, quiz_name Text

Question

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

answer

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

所以我的java类是:

public class Quiz {
    int idQuiz;
    String QuizName;

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

    // getters ...
}

我的问题类:

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多次选择此问题进行选择。

我有一个问题是获得真实答案的清单,用户必须选择所有真实答案,而不仅仅是一个如果有多个真正的答案。 有人对此有所了解吗? 感谢。

2 个答案:

答案 0 :(得分:0)

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

单选按钮只允许单选。复选框是您可以选择是否为每个选项选中它的控件。请注意,这也包括“未选中答案”选项(虽然可以在没有选择单选按钮的情况下启动,但一旦用户选择任何单选按钮,它们将无法取消选择)。

答案 1 :(得分:0)

我担心你需要Destructor The Destroyer Of Worlds的帮助。

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

首先,你需要这个非常重要的课程:

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!");
}