I’ve coded a simple quiz game for Android, and currently Im having troubles with making questions not appear after they’ve been shown, i.o. I dont want it to ask me the same question twice..
This is the method Im using
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
}
Im not sure if this will work but, what if I add something like
Edit : Doesn’t work..
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..");
}
Edit 2: Got told to add something like this, but I have NO IDEA what I should be doing with this now..
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!
}
The simplest approach would be to generate an
ArrayListof all the possible numbers you want to use, the shuffle them withCollection.shuffle. Then just iterate over the list.EDIT: Your question is really unclear now, given that you’ve said you don’t want the questions in a random order… whereas your sample code appears to be trying to present the questions in a random order. Anyway… here’s what I was suggesting: