Reputation: 27
I have an assignment to do so this assignment is a simple quiz program that the user need to answer the 10 questions from the database (but i currently have 3 questions displaying all in one frame because i used jscrollpane..). At the end of the quiz i'd like to display the score of the user but instead to display "3 out of 3" it displays "6 out of 3"..and i am really trying my best for almost 3 days to figure out why.
can anyone give me the solution to this problem?
I am using NetBeans.
public class Quiz extends javax.swing.JFrame {
ArrayList<Question> list = new ArrayList<>();
String answer = "";
String answer1 = "";
String answer2 = "";
int percent;
int score = 0;
String correct;
try {
ResultSet rs = stmt.executeQuery("Select * from Questions");
while (rs.next()) {
Question x = new Question(rs.getInt("ExamCode"), rs.getString("Category"),
rs.getString("Question"), rs.getString("ChoiceA"), rs.getString("ChoiceB"),
rs.getString("ChoiceC"), rs.getString("ChoiceD"), rs.getString("Answer"));
list.add(x);
for (int i = 0; i < list.size(); i++) {
correct = list.get(i).getAnswer();
if (correct.equalsIgnoreCase(answer)) {
score++;
System.out.print(score);
}
if (correct.equalsIgnoreCase(answer1)) {
score++;
System.out.print(score);
}
if (correct.equalsIgnoreCase(answer2)) {
score++;
System.out.print(score);
}
}
new Result(score, list).setVisible(true);
this.dispose();
}
} catch (SQLException ex) {
Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
}
//code in my resultform
public Result() {
initComponents();
}
public Result(int score, ArrayList<Question> list) {
initComponents();
labelResult.setText(String.valueOf(score + " out of " + list.size() + "!"));
}
Upvotes: 0
Views: 930
Reputation: 11543
When you read from the database you ad the result to a list and iterate over that list (for each addition). So first you read choice 1, loop over the list (containing 1j, then you add 2, loop over the list containing 1 and 2, then you add 3, and loop over the list containing 1, 2 and 3. So choice 1 will be scored 3 times, 2 2 times and 3 one time. Total of 6 scores.
However, there is another problem.
You compare each choice with all the answers. That means that the score will increment if it matches multiple answers.
Say I have answers a, b, b and I choose b, a, a. In your loop choice 1 will get 2 points (matching answer 2 and 3), and choice 2 and 3 gets one point each (matching answer 1).
You must only compare the choice with the answer for that question. Easiest is probably to put the answers in a list instead of separate variables.
As your code does not show how answer
answer1
answer2
are one general guidance is possible:
// Collect all answers
ResultSet rs = stmt.executeQuery("Select * from Questions");
while (rs.next()) {
Question x = new Question(rs.getInt("ExamCode"), rs.getString("Category"),
rs.getString("Question"), rs.getString("ChoiceA"), rs.getString("ChoiceB"),
rs.getString("ChoiceC"), rs.getString("ChoiceD"), rs.getString("Answer"));
list.add(x);
}
// Not sure how you collect answers...
List<String> answers = new ArrayList<>();
// Collect answers ...
answers.add("answer 1");
answers.add("answer 2");
answers.add("answer 3");
for (int i = 0; i < list.size(); i++) {
correct = list.get(i).getAnswer();
if (correct.equalsIgnoreCase(answers.get(i))) {
score++;
System.out.print(score);
}
}
// Show result
new Result(score, list).setVisible(true);
this.dispose();
}
Upvotes: 1