Skip to content Skip to sidebar Skip to footer

Why Does It Call Method 'iscorrect' Multiple Times (onclick) If I Hit The Button Only Once?

I made a simple quiz game for android, right now there's only 10 questions, and 40 answers. (4 answers for each question) Sometimes when I hit a button it gives me more than one co

Solution 1:

Consider the following changes and see if it improves:

privateint questionNumber = -1;

 privatevoidGame(int q){
    ...
    questionNumber = q;
    q_textview.setText(questions[q]);
    ...
 }

private boolean isCorrect(int button){
    if(button == 1 && a1.equals(answers_correct[questionNumber])
        || button == 2 && a2.equals(answers_correct[questionNumber])
        || button == 3 && a3.equals(answers_correct[questionNumber])
        || button == 4 && a4.equals(answers_correct[questionNumber]))
        returntrue;
    }
    returnfalse; 
}

Storing the question number as a field eliminates the for loop over all the questions. You should always use String.equals instead of ==.

Post a Comment for "Why Does It Call Method 'iscorrect' Multiple Times (onclick) If I Hit The Button Only Once?"