如何让它在4行之一上随机显示答案,没有任何重复?
我目前的代码:
TextView question;
private int qType = -1;
private int asked = 0;
private void QBegin() {
/*
* Gets a random question
*/
question = (TextView) findViewById(R.id.question);
String[] types = { "Q1", "Q2", "Q3", "Q4", "Q5"};
Random random = new Random();
int qType = random.nextInt(types.length);
question.setText(types[qType]);
asked++;
// StringList.add(types[qType]);
getAnswers(qType);
/* if(StringList.contains(types[qType]) && asked >= types.length+1){
asked = 0;
answerCounter.setText("THE END");
} else if (StringList.contains(types[qType]) && asked < types.length+1){
QBegin();
} */
}
public static int random(int range) {
return (int)(java.lang.Math.random() * (range+1));
}
public void shuffle(String input){
/*
* Unused shuffle method
*/
List<Character> characters = new ArrayList<Character>();
for(char c:input.toCharArray()){
characters.add(c);
}
StringBuilder output = new StringBuilder(input.length());
while(characters.size()!=0){
int randPicker = (int)(Math.random()*characters.size());
output.append(characters.remove(randPicker));
}
System.out.println(output.toString());
}
private void getAnswers(int Type) {
/*
* Getting answers here
*/
int randomValue = random(4);
try {
String answers_list[][] = {
{"Answer 1-1", "Answer 2-1", "Answer 3-1", "Answer 4-1"},
{"Answer 1-2", "Answer 2-2", "Answer 3-2", "Answer 4-2"},
{"Answer 1-3", "Answer 2-3", "Answer 3-3", "Answer 4-3"},
{"Answer 1-4", "Answer 2-4", "Answer 3-4", "Answer 4-4"},
{"Answer 1-5", "Answer 2-5", "Answer 3-5", "Answer 4-5"}} ;
answer1.setText(answers_list[Type][randomValue+1 > 3 ? (randomValue+0)-4 : randomValue+0]);
answer2.setText(answers_list[Type][randomValue+2 > 3 ? (randomValue+1)-3 : randomValue+1]);
answer3.setText(answers_list[Type][randomValue+3 > 3 ? (randomValue+2)-2 : randomValue+2]);
answer4.setText(answers_list[Type][randomValue+0 > 3 ? (randomValue+3)-4 : randomValue+3]);
/*for (int rows = 0; rows < answer&*list.length; rows++){
for (int cols = 0; cols < answers_list[rows].length; cols++){
}
}*/
} catch(Exception ex){
answer1.setText("Error "+ex);
}
}
负责挑选的代码&amp;在4行之一上添加问题(随机)
answer1.setText(answers_list[Type][randomValue+1 > 3 ? (randomValue+0)-4 : randomValue+0]);
answer2.setText(answers_list[Type][randomValue+2 > 3 ? (randomValue+1)-3 : randomValue+1]);
answer3.setText(answers_list[Type][randomValue+3 > 3 ? (randomValue+2)-2 : randomValue+2]);
answer4.setText(answers_list[Type][randomValue+0 > 3 ? (randomValue+3)-4 : randomValue+3]);
目前,我正在使用answer1和answer4重复。请帮助。
答案 0 :(得分:1)
有一种已知的标准改组方法。
您现在有一个随机列表,其中包含随机顺序的原始四个答案。该算法称为Fisher-Yates shuffle。
ETA简单示例代码:
Random rand = new Random();
String[] answers = { "42",
"Only on a Tuesday.",
"$4.36",
"Hieronymous K. Sluggenheimer III" };
// Shuffle answers[]
for (int i = 3; i > 0; --i) {
// Pick an answer that hasn't yet been chosen.
int pick = rand.nextInt(i + 1);
if (pick != i) {
// Exchange answers[i] and answers[pick].
String temp = answers[i];
answers[i] = answers[pick];
answers[pick] = temp;
}
}
答案 1 :(得分:1)
如果我正确理解了你的目标,那么你就是在试图随机改变一系列问题。 Rossum为您提供了一种方法,this是对最知名选项的一个很好的介绍。
在我看来,这是最简单的方法:
TreeMap <Double,String>
或<Double,Question>
)不要担心重复的随机数或将其规范化。重复是不太可能的,如果您需要问题#,请在循环中添加一个计数器。