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

时间:2017-06-23 17:40:30

标签: java swing windowbuilder

我正在使用WindowBuilder为学校作业创建一个多项选择测试。我差不多完成了,但是我在保持分数方面遇到了麻烦。分数似乎没有加起来。我正在使用两个类来创建多项选择测试。如果有人可以帮我设置得分系统,将非常感激。     `

public class 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));

}

}

公共课问题{

private String question;
private String choiceA;
private String choiceB;
private String choiceC;
private String choiceD;
private String question1;
private String question2;
private String question3;
private String question4;
private String question5;
private String question6;
private String question7;
private String question8;
private String question9;
private String question10;
private String answer2;
private int answer;
private int userChoice;
private int selected;
JRadioButton rbchoice1;
JRadioButton rbchoice2;
JRadioButton rbchoice3;
JRadioButton rbchoice4;

public Question(){
    question = "";

}

public void setQuestion(String q) {
    question = q;
}

public void setChoice(int c1, String cA) {
    if (c1 == 1) {
        choiceA = cA;
    }
    if (c1 == 2) {
        choiceB = cA;
    }
    if (c1 == 3) {
        choiceC = cA;
    }
    if (c1 == 4) {
        choiceD = cA;}
    }

    public void setQuestion(int c1, String q1) {
        if (c1 == 1) {
            question1 = q1;
        }
        if (c1 == 2) {
            question2 = q1;
        }
        if (c1 == 3) {
            question3 = q1;
        }
        if (c1 == 4) {
            question4 = q1;
        }
        if (c1 == 5) {
            question5 = q1;
        }
        if (c1 == 6) {
            question6 = q1;
        }
        if (c1 == 7) {
            question7 = q1;
        }
        if (c1 == 8) {
            question8 = q1;
        }
        if (c1 == 9) {
            question9 = q1;
        }
        if (c1 == 10) {
            question10 = q1;
        }
}

public void setAnswer(int a) {
    answer = a;

}

public void displayQuestion() {
    System.out.println(question);
    System.out.println("1. " + choiceA);
    System.out.println("2. " + choiceB);
    System.out.println("3. " + choiceC);
    System.out.println("4. " + choiceD);
}

public void getAnswer(int a) {
    answer = a;
    if (a == 1) {
        System.out.println("Incorrect!");
    } else if (a == 2) {
        System.out.println("Correct!");
    }
    else if (a == 3) {
        System.out.println("Incorrect!");
    }
    else    if (a == 4) {
        System.out.println("Incorrect!");
    } else {
        System.out.println("Please write a number between 1-4!");
    }

}

public String getChoice(int c1) {
    if (c1 == 1) {
        return choiceA;
    }
    if (c1 == 2) {
        return choiceB;
    }
    if (c1 == 3) {
        return choiceC;     }
    if (c1 == 4) {
        return choiceD;
    }
    return "error";

}
public String getQuestion(int q1) {
    if (q1 == 1) {
        return question1;
    }
    if (q1 == 2) {
        return question2;
    }
    if (q1 == 3) {
        return question3;       }
    if (q1 == 4) {
        return question4;
    }
    if (q1 == 5) {
        return question5;
    }
    if (q1 == 6) {
        return question6;
    }
    if (q1 == 7) {
        return question7;
    }
    if (q1 == 8) {
        return question8;
    }
    if (q1 == 9) {
        return question9;
    }
    if (q1 == 10) {
        return question10;
    }
    return "error";

}

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

public void displayEnd(int score){

}

} `

2 个答案:

答案 0 :(得分:2)

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

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

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

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

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

group.clearSelection();
questionNum++;

到nextQuestion()方法

你的actionPerformed方法不应该&#34;知道&#34;关于每个问题的正确答案是什么。此信息已在问题类中提供,应从那里检索。

答案 1 :(得分:0)

有一些错误..

首先你有两个q10 = new Question();,一个应该是

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();的最后一部分致电else if(questionNum),如下所示

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