带分数计数器的Java数据库测验

时间:2015-03-27 17:46:47

标签: java mysql

每次在我所做的测验中点击正确的答案时,我一直在尝试增加一个计数。我明白问题在于,计算得分的if语句发生在按钮的末尾。 s动作事件,因此它将计入下一个答案,而不是当前答案。

我认为这很容易修复,我可以将上面的if语句移动到我正在洗牌的地方(见下文)但是这在任何位置都不起作用,程序将在正确答案时返回异常遇到。如果有人能帮助我那会很酷。

我也意识到我可能在这里有点模糊,但我可以试着解释一些没有用的东西。

private void answerBtn1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:   
       try {

            Class.forName("com.mysql.jdbc.Driver");
            String connectionUrl = "jdbc:mysql://localhost:3306/jdbcTest?user=test&password=hihi";
            Connection conn = DriverManager.getConnection(connectionUrl);
            java.sql.Statement st = (java.sql.Statement) conn.createStatement();
            String find = "select quest,answer,wrongAns,wrongAns2,wrongAns3,q_sel from Question where country = 'ireland' and q_sel = 'N' order by rand() limit 1;"; 
            ResultSet rs = st.executeQuery(find);

            /* https://www.youtube.com/watch?v=MY4FavUyFNQ */ 

            while(rs.next()){
              question = questionLbl.getText();
              questionLbl.setText("\n"+rs.getString("quest")); 
              java.sql.Statement st2 = conn.createStatement();
              st2.executeUpdate("update Question set q_sel = 'Y' where quest = '"+rs.getString("quest")+"'");

              String[] solutionArray = {rs.getString("answer"),rs.getString("wrongAns"),rs.getString("wrongAns2"),rs.getString("wrongAns3")};

              arr = solutionArray;
              count2++;

              Random rgen = new Random();
              int N = arr.length;

            /*
            * fisher yates shuffle
            *
            * 9/3/2015
            *
            * @reference http://algs4.cs.princeton.edu/11model/Knuth.java.html
            * @author sean trant 13332576
            */ 

              for (int i = 0; i < N; i++) {
                // choose index uniformly in [i, N-1]
                int r = i + (int) (Math.random() * (N - i));

                Object swap = arr[r];
                arr[r] = arr[i];
                arr[i] = (String)swap;

                answerBtn1.setText(arr[0]);
                answerBtn2.setText(arr[1]);
                answerBtn3.setText(arr[2]);
                answerBtn4.setText(arr[3]); 
            }

             if(arr[0].equals(rs.getString("answer"))){
                count++;
                scoreLbl.setText("Score:"+count);
                }     
            }

            } catch (Exception ex) {
            System.out.println("error"); 
            }

    }         

0 个答案:

没有答案