首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使JRadioButton随机

使JRadioButton随机
EN

Stack Overflow用户
提问于 2016-08-19 04:13:24
回答 2查看 394关注 0票数 1

我的数据来自对单选按钮的查询,如下所示:

代码语言:javascript
复制
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

在这种情况下,每次都将正确答案分配给第三个单选按钮,这使得测验可以预测。我可以这样做吗?正确的选择有时分配给第一个电台,有些时候分配给第二个,以此类推?

EN

回答 2

Stack Overflow用户

发布于 2016-08-19 04:33:28

您可以执行以下操作:

代码语言:javascript
复制
String one = Question.get(i).getChoiceOne();
String two = Question.get(i).getChoiceTwo();
String three = Question.get(i).getcorrectchoice();

// construct list from questions.
List<String> choices = Arrays.asList(one, two, three);

// give random order to list
Collections.shuffle(choices);

// set radio button texts
radio1.setText(choices.get(0));
radio2.setText(choices.get(1));
radio3.setText(choices.get(2));
票数 1
EN

Stack Overflow用户

发布于 2016-08-19 16:55:51

代码语言:javascript
复制
      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 
      }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39026745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档