0

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");
  }
}
Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
Abs90
  • 37
  • 1
  • 8
  • Cant help with your answer but the whole multiple ifs testing the same variable is a perfect case for a switch statement – ncremins Mar 03 '12 at 11:53

3 Answers3

0

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);
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
  • hey thanks for the reply. How do I pass the index in the getNextQuestion() method and increment the index? – Abs90 Mar 05 '12 at 18:23
0

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())
Anordil
  • 160
  • 8
  • Or justhave a look there: http://stackoverflow.com/questions/9420491/random-math-questions-generator-for-android – Anordil Mar 03 '12 at 12:06
  • I wish to add a score counter if the question is correct. Where would I implement that? – Abs90 Mar 06 '12 at 12:32
0

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.

  • How do I get the next textview and edittext to overwrite the old ones? – Abs90 Mar 05 '12 at 17:38
  • There is a similar work on this site. [link](http://www.warriorpoint.com/blog/2009/05/29/android-switching-screens-by-dragging-over-the-touch-screen/) ` ` For example, above the text of the text view is Hello. You will write the question there. Actually there is a better way. You should create your questions in string.xml and referance it in textview like `android:text="@string/question1"` – Emrullah Kızıltepe Mar 06 '12 at 08:07
  • Don't forget that they change the screen by touching. You will not use that way. You will add click events for each button you have, and put the code I wrote in my first answer into the click events with corresponding textview id. – Emrullah Kızıltepe Mar 06 '12 at 08:16