使JRadioButton随机

时间:2016-08-18 20:13:24

标签: java swing random jradiobutton

我的数据来自这样的查询到我的单选按钮:

radio1.setText(Question.get(i).getChoiceOne()) // assign ist choice to ist radio button
radio2.setText(Question.get(i).getChoiceTwo()) // assign second choice to second radio
radio3.setText(Question.get(i).getcorrectchoice()) // assign correct choice to third radio button

在这种情况下,每次将正确的答案分配给第三个单选按钮,这使得测验可以预测。我能以某种方式进行,其中正确的选择有时会分配给第一个无线电,有时会分配给第二个,依此类推?

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

(slack is a messaging platform for teams, if you don't know)

答案 1 :(得分:0)

      int i = (Math.random() * 1000)%3;// generate a values between 0-2
      String[] answers = new String[3];
      answers[i] = Question.get(i).getcorrectchoice();// i is the index of right answer

      // insert the 2 other answers, what ever i you chose (i+1)%3 will put them in the remaining 2 places  
      answer[(i+1)%3] = Question.get(i).getfirstchoice(); 
      answer[(i+1)%3] = Question.get(i).getsecondchoice();
      i = (i+1)%3;// the original value of i
      // insert radios
      radio1.setText(answers[0]);
      radio2.setText(answers[1]);
      radio3.setText(answers[2]);

      //  get the right answer; get the text of the selected radio 
      if(textOfselectedRadio.quals(answers[i])){
      // correct 
      }