Ale*_*lex 0 java eclipse android
我为Android编写了一个简单的问答游戏,目前我在问题显示后没有出现问题,我不想让它两次问我同样的问题.
这是我使用的方法
private void QBegin() {
/*
* Gets a random question
*/
question = (TextView) findViewById(R.id.question);
String[] types = { "Question one",
"Question two",
"Question three",
"Question four",
"Question five"};
Random random = new Random();
int qType = random.nextInt(types.length);
question.setText(types[qType]);
getAnswers(qType); //gets four answers
}
Run Code Online (Sandbox Code Playgroud)
我不确定这是否可行但是,如果我添加类似的东西怎么办?
编辑:不起作用..
int i = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(qType);
i++;
if(list.contains(qType) && i != types.length + 1){
return;
} else {
answerCounter.setText("Hit the bricks pal, you're done..");
}
Run Code Online (Sandbox Code Playgroud)
编辑2:被告知添加这样的东西,但我没有想法我现在应该做什么...
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(types.length);
Collections.shuffle(list);
if(!list.contains(qType)){
// help please, as I have no idea on what I should be doing!
}
Run Code Online (Sandbox Code Playgroud)
最简单的方法是生成一个ArrayList你想要使用的所有可能数字,然后将它们随机播放Collection.shuffle.然后迭代列表.
编辑:你的问题是真的不清楚了,因为你说你不希望在一个随机顺序的问题......而出现的示例代码,试图呈现的问题在一个随机顺序.无论如何......这就是我的建议:
List<Integer> indexes = new ArrayList<Integer>();
for (int i = 0; i < types.length; i++) {
indexes.add(i);
}
Collections.shuffle(indexes);
for (Integer index : indexes) {
question.setText(types[index]);
getAnswers(index);
// Now do something else? It's unclear...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
808 次 |
| 最近记录: |