使用Java的RadioButton值递增递减

使用Java的RadioButton值递增递减

这是一个在线考试系统。如果答案是正确的标记,那么将标记加1即可在第二次相同的问题上正常工作。第二次选择错误的答案,我需要将值减一

  int marks;
    String cor;
        public void answerCheck()
            {
                String answerAnswer="";

                if(r1.isSelected())
                {
                    answerAnswer = r1.getText();

                }

               else if(r2.isSelected())
                {
                    answerAnswer = r2.getText();

                }

                   else if(r3.isSelected())
                {
                    answerAnswer = r3.getText();

                }

                    else if(r4.isSelected())
                {

                    answerAnswer = r4.getText();

                }

                if(answerAnswer.equals(cor))
                {
                    marks = marks + 1;
                    String Marks = String.valueOf(marks);
                    txtc.setText(Marks);
                }
                else if(!answerAnswer.equals(cor))
                {
                     marks = marks - 1;
                    String Marks = String.valueOf(marks);
                    txtc.setText(Marks);

                }
                else
                {
                    marks =0;
                }

            }


我正在从数据库中加载所有数据正确答案我也在加载
数据库加载

public void Connection()
   {


             Class.forName("com.mysql.jdbc.Driver");
             con = DriverManager.getConnection("jdbc:mysql://localhost/onlineex","root","");
               String query = "select * from questions";
       pst = con.prepareStatement(query);

             rs = pst.executeQuery();

                  while(rs.next())
            {
                txtqu.setText(rs.getString("id"));
                txtques.setText(rs.getString("question"));
                r1.setText(rs.getString(3));
                r2.setText(rs.getString(4));
                r3.setText(rs.getString(5));
                r4.setText(rs.getString(6));
                 cor = rs.getString(7);
            }

             }


接下来我有一个按钮呼叫

 try
           {

            if(rs.previous())
            {

                txtques.setText(rs.getString("question"));
                r1.setText(rs.getString(3));
                r2.setText(rs.getString(4));
                r3.setText(rs.getString(5));
                r4.setText(rs.getString(6));

            cor=rs.getString(7);
            }
            else
                {
                    JOptionPane.showMessageDialog(this, "This is first record of student");
                }
        answerCheck();
      }


我有以前的按钮呼叫

if(rs.next())
        {

            txtques.setText(rs.getString("question"));
            r1.setText(rs.getString(3));
            r2.setText(rs.getString(4));
            r3.setText(rs.getString(5));
            r4.setText(rs.getString(6));

        cor=rs.getString(7);
        }
        else
            {
                JOptionPane.showMessageDialog(this, "This is first record of student");
            }
    answerCheck();


java - 使用Java的RadioButton值递增递减-LMLPHP

最佳答案

首先,最后一个逻辑错误将永远不会执行,因为答案是正确的还是错误的,没有第三个条件。

String cor;
    public void answerCheck()
        {
            String answerAnswer="";

            if(r1.isSelected())
            {
                answerAnswer = r1.getText();

            }

           else if(r2.isSelected())
            {
                answerAnswer = r2.getText();

            }

               else if(r3.isSelected())
            {
                answerAnswer = r3.getText();

            }

                else if(r4.isSelected())
            {

                answerAnswer = r4.getText();

            }

            if(answerAnswer.equals(cor))
            {
                marks = marks + 1;
                String Marks = String.valueOf(marks);
                txtc.setText(Marks);
            }
            else if(!answerAnswer.equals(cor) || ((r1.isSelected() ||
                    r2.isSelected() || r3.isSelected() || r4.isSelected()))
            {
                 marks = marks - 1;
                String Marks = String.valueOf(marks);
                txtc.setText(Marks);

            }

}

关于java - 使用Java的RadioButton值递增递减,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61780314/

10-10 20:09