Tanguy MP Tanguy MP - 4 months ago 11
Java Question

After all questions are displayed one-by-one, I want the 1st message displayed again

On Android, after the last question ( "Who's Flavie?"), I want the first question("Who's Tom?") displayed again, when I click on the button

final String questions[] = {"Who's Tom?", "Who's Luca?", "Who's Flavie?"};
final String answers[] = {"American", "Italian", "French"};

// display question
answer_question.setOnClickListener(new View.OnClickListener() {
int CurrentQuestionIndex = 0;
public void onClick(View v) {

ask_question.setText(question[(CurrentQuestionIndex++) % (questions.length)]);
// discuss question versus answer
EditText editText = (EditText) findViewById(R.id.tvReponseF);
if(editText.getText().toString().equals(answers[CurrentQuestionIndex]))
{
alertMessageRight();
} else {
alertMessageFalse();
}
}

Answer

complex approach

 final String questions[] = {"Who's Tom?", "Who's Luca?", "Who's Flavie?"}
 final String answers[] = {"American", "Italian", "French"}

 // display question
answer_question.setOnClickListener(new View.OnClickListener() {
int CurrentQuestionIndex = 0;
public void onClick(View v) {
// make below line changes
ask_question.setText(question[(++CurrentQuestionIndex) % (questions.length-1)]);
// discuss question versus answer
EditText editText = (EditText) findViewById(R.id.tvReponseF);
if(editText.getText().toString().equals(answers[CurrentQuestionIndex]))  
{
    alertMessageRight();
} else {
    alertMessageFalse();


  }
} 
});

simple approach

final String questions[] = {"Who's Tom?", "Who's Luca?", "Who's Flavie?"}
 final String answers[] = {"American", "Italian", "French"}

 // display question
answer_question.setOnClickListener(new View.OnClickListener() {
int CurrentQuestionIndex = 0;
public void onClick(View v) {

ask_question.setText(question[CurrentQuestionIndex]);

// discuss question versus answer
EditText editText = (EditText) findViewById(R.id.tvReponseF);
if(editText.getText().toString().equals(answers[CurrentQuestionIndex]))  
{
    alertMessageRight();
} else {
    alertMessageFalse();


  }


 if(CurrentQuestionIndex++ == questions.length-1){
CurrentQuestionIndex =0;
      }
    } 

    });
Comments