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() + "!"));
}