简体   繁体   中英

How to Loop over ten questions

I'm in the process of creating a simple maths game. I'm trying to create a loop that will display ten questions, one at a time, to a textview. The user will enter the answer into an edittext, press a button, and then question will be validated and the next question in the series of ten will appear in the textview.

I'm just not quite sure how to get to the next question. Any help will be greatly appreciated.

Here's what I've done so far:

int x = 0;

while (x < 10) {
  if (i1 == 1) {
    answer = q1;
    editTextEquation.setText(random1 + "+" + random2);
    x++;
  }
  if (i1 == 2) {
    answer = q2;
    editTextEquation.setText(random1 + "-" + random2);
    x++;
  }
  if (i1 == 3) {
    answer = q3;
    editTextEquation.setText(random1 + "/" + random2);
    x++;
  }
  if (i1 == 4) {
    answer = q4;
    editTextEquation.setText(random1 + "*" + random2);
    x++;
  }
}

and then the validation button calls this method

private void questions() {
  int score = 0;
  int i = Integer.parseInt(editText.getText().toString());
  if (i == answer) {
    editText.setText("Correct");
    score++;
  } else {
    editText.setText("Incorrect");
  }
}

Have a method getNextQuestion() and call it in the button click Listener. Just change the Text in the TextView by calling

TextView.setText(Question)

Have an ArrayList of Question and pass the index or count in the getNextQuestion() method to show the next question and increment the index/count after that.

Edit: Set a field in your Activity as

int index=0;

and pass this index in your method, which is changing the text.

 getNextQuestion(index);

I'd suggest the following (assuming you want to generate each question randomly):

  • Define a String getQuestion() method that will return your randomly generated question (two random number and a random operand) in a String
  • Define a boolean checkAnswer(String question, String answer)
    that will check the provided answer is right for the provided
    question.

Then, when clicking your "validate" button, you

  • call

    checkAnswer(yourQuestionsTextView.getText().toString(), yourAnswerField.getText().toString())

    and display "Correct" or "False" using Toast depending on the result

  • call yourQuestionsTextView.setText(getQuestion())

You can use Creator 's way or change the layout with the click event. Every layout will have its own textview and button. You will have just one xml and viewflipper in it. Put linear or relative layouts as many as your questions and textview and button in that layout. In the java file, put

ViewFlipper vf = (ViewFlipper) findViewById(R.id.ID_OF_VIEWFLIPPER);

vf.showNext();

into the click events of buttons. Then it will change the layout and next question will be shown.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM