I'm creating android quiz app. I have randomized its question but sometimes the generated questions are repeated.
How to prevent its repetition and terminate its method after all the questions are displayed?
Here's the whole working code:
public class MainActivity extends Activity {
private int currentQuestion;
private String [] questions;
private String [] answers;
private Button answerButton;
private Button questionButton;
private TextView questionView;
private TextView answerView;
private EditText answerText;
Random random = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init() {
questions = new String[]{"Q1?","Q2","Q3","Q4","Q5","Q6","Q7","Q8","Q9","Q10"};
currentQuestion = random.nextInt(questions.length);
answers = new String[]{"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"};
answerButton = (Button)findViewById(R.id.AnswerButton);
questionButton = (Button)findViewById(R.id.QuestionButton);
questionView = (TextView) findViewById(R.id.QuestionTextView);
answerView = (TextView) findViewById(R.id.AnswerTextView);
answerText = (EditText) findViewById(R.id.AnswerText);
answerButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {checkAnswer();
}});
questionButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
showQuestion();
}});
}
public int showQuestion(){
currentQuestion++;
if(currentQuestion == questions.length);
currentQuestion = 0;
currentQuestion = (random.nextInt(questions.length));
questionView.setText(questions[currentQuestion]);
answerView.setText("");
answerText.setText("");
return currentQuestion;
}
public void checkAnswer() {
String answer = answerText.getText().toString();
if(isCorrect(answer))
answerView.setText("You're right!");
else answerView.setText("Sorry, the correct answer is "+answers[currentQuestion]);
}
}
Use a Fisher–Yates shuffle to shuffle questions
and answers
in advance of using them.
static void fyShuffle(String[] questions, String[] answers)
{
Random rnd = new Random();
for (int i = questions.length - 1; i > 0; --i){
int index = rnd.nextInt(i + 1);
// Swap questions
String s = questions[index];
questions[index] = questions[i];
questions[i] = s;
// Swap answers
s = answers[index];
answers[index] = answers[i];
answers[i] = s;
}
}
See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
It has the desirable property that after initialisation (which is O(N)), array access time is constant. Algorithms that reject re-drawn elements tend to slow down and so don't scale well.
you can use a collection that does not hold duplicates ( HashSet
) for instance, and use it instead of a String[]
. If you want the String[] array you can easily create it from the HashSet's content
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.