我的数据来自对单选按钮的查询,如下所示:
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在这种情况下,每次都将正确答案分配给第三个单选按钮,这使得测验可以预测。我可以这样做吗?正确的选择有时分配给第一个电台,有些时候分配给第二个,以此类推?
发布于 2016-08-19 04:33:28
您可以执行以下操作:
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));发布于 2016-08-19 16:55:51
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
}https://stackoverflow.com/questions/39026745
复制相似问题