0

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() + "!"));
     }
catmer
  • 27
  • 5
  • Where are you initializing the variable "answer", "answer1" and "answer2". Especially if any of those are equal during your for-loop, then you can score can increase by more than one each iteration. – user2089648 Feb 29 '20 at 07:02

1 Answers1

1

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();
}
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
  • Thank you for your answer, as you guessed, I'm a beginner.. can you please give me an example of the list you were saying coz' i tried to put the answers in a list as what you've said but the code, part in my radiobutton contains error "Incompatible types: String cannot be converted to ArrayList" . I'm not sure if what you've said matches what i've do please help me..I'm sorry if this is rather annoying. – catmer Feb 29 '20 at 08:01
  • I am really thankful to you Sir @Roger but i tried the code you've shown but it doesn't count any correct answers.. i hope you have long patience. – catmer Feb 29 '20 at 12:34
  • You haven't shown how you collect the answers (and put them in the list). Perhaps you should try to step through your program using a debugger? – Roger Lindsjö Feb 29 '20 at 12:41
  • List answers = new ArrayList<>(); Question q1 = new Question(45678, "Easy", "Who isme?", "ma", "me", "mi", "mo", "me"); Question q2 = new Question(5678, "Easy", "Who is you?", "y", "o", "u", "you", "you"); Question q3 = new Question(6789, "Easy", "Who is her?", "h", "e", "r", "her", "her"); answers.add(q1); answers.add(q2); answers.add(q3); //is this what you mean sir for 'collect the answers' ? – catmer Feb 29 '20 at 13:38
  • No, I'm assuming the user somehow enters / selects an answer, that ring should be collected in the answers list. Then you compare `list.get(i).getAnswer().equalsIgnoreCase(answers.get(i))` (so you compare the known correct answer for the question with the user input). – Roger Lindsjö Feb 29 '20 at 16:54