vendredi 20 février 2015

Repeated random number questions

I have the following class which needs to display random questions to the user.


However my issue is that the class displays only one question rather than asking questions continuously . How can I repeat user questions in order to keep their! score? See image below - enter image description here



public class Chooser extends ActionBarActivity implements View.OnClickListener {

int randomOne = 0;
int randomTwo = 0;
int problems = 0;
int sum = 0;
int userAnswer = 0;
int correct = 0;
int inCorrect = 0;
int userInput, result;

TextView question, response, answer, report;
EditText userNumber;
Button ans;
private Random add;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chooser);

question = (TextView) findViewById(R.id.question);
response = (TextView) findViewById(R.id.response);
answer = (TextView) findViewById(R.id.answers);
report = (TextView) findViewById(R.id.report);
userNumber = (EditText) findViewById(R.id.answer);
ans = (Button) findViewById(R.id.button);
ans.setOnClickListener(this);
add = new Random();
randomOne = add.nextInt(20);
randomTwo = add.nextInt(10);
Display();

}

public void Display() {
sum = randomOne + randomTwo;
question.setText(randomOne + " + " + randomTwo);
}

@Override
public void onClick(View v) {
userInput = Integer.parseInt(userNumber.getText().toString());
Log.e("getting user input", "" + userInput);

if (userInput == sum) {

response.append(" CORRECT! , You are the best \n");

correct++;
} else {

response.append("INCORRECT, The correct answer was :" + sum + "\n");

inCorrect++;
}

problems++;


}


}




Aucun commentaire:

Enregistrer un commentaire