简体   繁体   中英

checking if answer right or wrong

I have a project which is an quiz that has multiple questions, true/false and fill in the blank, my problem is with the Check answer method.

First I have written the Q's in database with SQL and the columns is as follow enter image description here

and there are 2 methods the first one is for retrieving the questions, choices and right answer

public void m() {
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost/quiz?user=root&password=Manager@1");
        Statement stm = con.createStatement();
        ResultSet rs1 = stm.executeQuery("Select * from quiz.qs where qid="+ Random());
            
        if (rs1.next()) {
            bg.clearSelection();
            quest.setText(rs1.getString(2));
            r1.setText(rs1.getString(3));
            r2.setText(rs1.getString(4));
            r3.setText(rs1.getString(5));
            r4.setText(rs1.getString(6));
            answer = rs1.getString(7);
            check(answer);
        }
    }
    catch(Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

the second one is for checking if the chosen answer was right, that's the part that I have problem with

public void check(String ans) {
    boolean works=false;
    String stuans="";
    if (r1.isSelected()) {
        if (ans.equals(r1.getText())) {
            marks=marks+1;
        }
    } else if (r2.isSelected()) {
        if (ans.equals(r2.getText())) {
            marks=marks+1;   
        }
    } else if (r3.isSelected()) {
        if (ans.equals(r3.getText())) {
            marks=marks+1;  
        }
    } else if (r4.isSelected()) {
        if (ans.equals(r4.getText())) {
            marks=marks+1;
        }
    }
    System.out.println(marks);// to check if marks has increased after checking
}

whenever I choose the right answer, the marks does not increase.

enter image description here

that's a pic from the program with GUI, sorry if something is not so clear

Don't use == with Strings. Use the.equals method.

How do I compare strings in Java?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM