使用WindowBuilder(Java)创建多项选择测试

使用WindowBuilder(Java)创建多项选择测试,java,swing,windowbuilder,Java,Swing,Windowbuilder,我正在使用WindowBuilder为学校作业创建多项选择题测试。我几乎完成了,但是,我在记分方面遇到了困难。分数似乎不合算。我使用两个类来创建选择题测试。如果有人能帮我建立评分系统,我将不胜感激。 ` 公共类test2扩展了JFrame{ private JPanel contentPane; JLabel labelQuestion; Question q1; Question q2; Question q3; Question q4; Question q5; Question q6; Q

我正在使用WindowBuilder为学校作业创建多项选择题测试。我几乎完成了,但是,我在记分方面遇到了困难。分数似乎不合算。我使用两个类来创建选择题测试。如果有人能帮我建立评分系统,我将不胜感激。 `

公共类test2扩展了JFrame{

private JPanel contentPane;
JLabel labelQuestion;
Question q1;
Question q2;
Question q3;
Question q4;
Question q5;
Question q6;
Question q7;
Question q8;
Question q9;
Question q10;
Question q11;
JRadioButton rbchoice1;
JRadioButton rbchoice2;
JRadioButton rbchoice3;
JRadioButton rbchoice4;
int questionNum = 1;
int score = 0;
int selected;

// Question q1 = new Question();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test2 frame = new test2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public void initializeQuestions() {
    q1 = new Question();
    q1.setQuestion(1, "Which statement generates a random number between 1 and 100?");
    q1.setChoice(1, "number = Math.random() * 100 + 1;");
    q1.setChoice(2, "number = Math.random() * 100 - 1;");
    q1.setChoice(3, "number = Math.random() * 99 + 1;");
    q1.setChoice(4, "number = Math.random(100);");
    q1.setAnswer(1);

    q2 = new Question();
    q2.setQuestion(1, "Which of the following variable names is NOT valid?");
    q2.setChoice(1, "pShape");
    q2.setChoice(2, "ThirdClass");
    q2.setChoice(3, "3rdClass");
    q2.setChoice(4, "discount");
    q2.setAnswer(3);

    q3 = new Question();
    q3.setQuestion(1, "Which of the following is one of the Java primitive types?");
    q3.setChoice(1, "boolean");
    q3.setChoice(2, "double");
    q3.setChoice(3, "int");
    q3.setChoice(4, "all of the above");
    q3.setAnswer(4);

    q4 = new Question();
    q4.setQuestion(1, "Which of the following is not a primitive data type?");
    q4.setChoice(1, "float");
    q4.setChoice(2, "double");
    q4.setChoice(3, "char");
    q4.setChoice(4, "String");
    q4.setAnswer(4);

    q5 = new Question();
    q5.setQuestion(1, "Which statement must be included to use Math.PI in your program?");
    q5.setChoice(1, "import java.lang.*;");
    q5.setChoice(2, "import java.util.*;");
    q5.setChoice(3, "public class Math");
    q5.setChoice(4, "public static void main()");
    q5.setAnswer(1);

    q6 = new Question();
    q6.setQuestion(1, "Which statement will increment total by 1?");
    q6.setChoice(1, "total = 1;");
    q6.setChoice(2, "total++;");
    q6.setChoice(3, "total--;");
    q6.setChoice(4, "total += total;");
    q6.setAnswer(2);

    q7 = new Question();
    q7.setQuestion(1, "The statement x += 2; is equivalent to?");
    q7.setChoice(1, "x = x + 2;");
    q7.setChoice(2, "x = x + 1 = x + 1;");
    q7.setChoice(3, "++x;");
    q7.setChoice(4, "x++;");
    q7.setAnswer(1);

    q8 = new Question();
    q8.setQuestion(1, "In a switch statement what must come after every case?");
    q8.setChoice(1, "switch");
    q8.setChoice(2, "break");
    q8.setChoice(3, "default");
    q8.setChoice(4, "cout");
    q8.setAnswer(2);

    q9 = new Question();
    q9.setQuestion(1, "Which of the following for headers is not valid?");
    q9.setChoice(1, "for ( int i = 0; i < 10; i++ )");
    q9.setChoice(2, "int i = 0; for ( ; i < 10; i++ )");
    q9.setChoice(3, "for ( int i = 0; int j = 5; ; i++ )");
    q9.setChoice(4, "All of the above");
    q9.setAnswer(3);

    q10 = new Question();
    q10.setQuestion(1, "An array is not:");
    q10.setChoice(1, "a consecutive group of memory locations.");
    q10.setChoice(2, "subscripted by integers.");
    q10.setChoice(3, "declared using braces, [].");
    q10.setChoice(4, "made up of different data types.");
    q10.setAnswer(4);

    q10 = new Question();
    q10.setQuestion(1, "Your score is: " + score + "/10");
    q10.setChoice(1, "");
    q10.setChoice(2, "");
    q10.setChoice(3, "");
    q10.setChoice(4, "");
    q10.setAnswer(4);
}

/**
 * place the question data in the JLabel and RadioButtons
 */

/**
 * Create the frame.
 */
public test2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblJavaQuestionsMultiple = new JLabel("       Java Multiple Choice Test!");
    lblJavaQuestionsMultiple.setBounds(138, 11, 192, 14);
    contentPane.add(lblJavaQuestionsMultiple);

    labelQuestion = new JLabel("");
    labelQuestion.setToolTipText("question");
    labelQuestion.setBounds(10, 57, 419, 14);
    contentPane.add(labelQuestion);

    rbchoice1 = new JRadioButton("");
    rbchoice1.setBounds(111, 77, 275, 21);
    contentPane.add(rbchoice1);

    rbchoice2 = new JRadioButton("");
    rbchoice2.setBounds(111, 104, 275, 21);
    contentPane.add(rbchoice2);

    rbchoice3 = new JRadioButton("");
    rbchoice3.setBounds(111, 131, 275, 21);
    contentPane.add(rbchoice3);

    rbchoice4 = new JRadioButton("");
    rbchoice4.setBounds(111, 158, 275, 21);
    contentPane.add(rbchoice4);
    // rbchoiceD.isSelected()

    ButtonGroup group = new ButtonGroup();
    group.add(rbchoice1);
    group.add(rbchoice2);
    group.add(rbchoice3);
    group.add(rbchoice4);

    JButton btnNext = new JButton("Next");
    btnNext.setBounds(336, 235, 93, 23);
    btnNext.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            // nextQuestion( ??? );

            if (rbchoice1.isSelected()) {
                selected = 1;
            } else if (rbchoice2.isSelected()) {
                selected = 2;
            } else if (rbchoice3.isSelected()) {
                selected = 3;
            } else if (rbchoice4.isSelected()) {
                selected = 5;
            }

            if (questionNum == 1 && selected == 1) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 2 && selected == 3) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 3 && selected == 4) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 4 && selected == 4) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 5 && selected == 1) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 6 && selected == 2) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 7 && selected == 1) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 8 && selected == 2) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 9 && selected == 3) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else if (questionNum == 10 && selected == 4) {
                JOptionPane.showMessageDialog(null, "Correct");
                score++;
            } else {
                JOptionPane.showMessageDialog(null, "Incorrect");
            }

            if (questionNum == 1) {
                nextQuestion(q2);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 2) {
                nextQuestion(q3);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 3) {
                nextQuestion(q4);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 4) {
                nextQuestion(q5);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 5) {
                nextQuestion(q6);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 6) {
                nextQuestion(q7);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 7) {
                nextQuestion(q8);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 8) {
                nextQuestion(q9);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 9) {
                nextQuestion(q10);
                group.clearSelection();
                questionNum++;
            } else if (questionNum == 10)
                nextQuestion(q11);
                group.clearSelection();
            }
    });
    contentPane.add(btnNext);

    JLabel scoreLabel = new JLabel("");
    scoreLabel.setBounds(201, 208, 46, 14);
    contentPane.add(scoreLabel);

    initializeQuestions();

    nextQuestion(q1);

}

public void nextQuestion(Question q) {

    // labelQuestion.setText( q.getQuestion() );

    labelQuestion.setText(q.getQuestion(1));
    rbchoice1.setText(q.getChoice(1));
    rbchoice2.setText(q.getChoice(2));
    rbchoice3.setText(q.getChoice(3));
    rbchoice4.setText(q.getChoice(4));

}
}
`

你真的应该考虑使用某种集合来存储你的问题,而不是每个问题都有一个变量

然后我想知道为什么你的问题文本有一个索引。每个问题只有一个问题,整个软件似乎不可调整,以支持未来的多个问题

为什么您的问题类对每个问题都有一个变量,但在initializeQuestions()方法中创建了10个实例

在actionPerformed()方法中,当选择第四个按钮时,将selected设置为5。这也可能是你最后的计数不正确的原因

您还应该通过移动来限制一些冗余

group.clearSelection();
questionNum++;
到nextQuestion()方法


你的actionPerformed方法不应该“知道”每个问题的正确答案。此信息已在问题类中可用,应从中检索。

存在一些错误

首先你有两个
q10=新问题()一个应该是

q11 = new Question();
    q11.setQuestion(1, "Your score is: " + score + "/10");
    q11.setChoice(1, "");
    q11.setChoice(2, "");
    q11.setChoice(3, "");
    q11.setChoice(4, "");
    q11.setAnswer(4);
然后您应该调用
initializeQuestions()
中的最后一部分,如果(questionNum)
如下

else if (questionNum == 10) {
     System.out.println(score);
     initializeQuestions();
     nextQuestion(q11);
 }

不要像你现在那样把数据和代码混为一谈。这会导致创建非常不灵活、难以增强和调试的程序。事实上,最重要的是,您的整个问题类都应该重新完成,使其包含单个问题的数据和行为,而不是所有问题。
else if (questionNum == 10) {
     System.out.println(score);
     initializeQuestions();
     nextQuestion(q11);
 }