Erstellen Sie einen Multiple -Choice -Test mit Windowbuilder (Java)Java

Java-Forum
Anonymous
 Erstellen Sie einen Multiple -Choice -Test mit Windowbuilder (Java)

Post by Anonymous »

Ich erstelle einen Multiple -Choice -Test mit Windowbuilder für einen Schulauftrag. Ich bin fast fertig, aber ich habe Probleme, die Punktzahl zu halten. Die Punktzahl scheint sich nicht zu summieren. Ich verwende zwei Klassen, um den Multiple -Choice -Test zu erstellen. Wenn mir jemand helfen kann, das Score -System einzurichten, das sehr geschätzt wird.
`< /p>

public class test2 erweitert Jframe {< /p>

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

}
< /code>

} < /p>

Public Class Frage {< /p>

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){

}
< /code>

}
`< /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post