mardi 3 mars 2015

Guessing game - comparing input number to computers generated number

I'm trying to develop an app that compares the user's input number to a randomly generated number from the computer, depending on whether the input number is higher, lower or the same as the generated number, there is a different message output. However the app crashes whenever I try and run it. Here is my code for my method:


If anyone can recognise why it's crashing it would be great - new to Android so it's hard to see errors.



public void guessingGame (View v)
{
EditText guess = (EditText) findViewById(R.id.ETguess);
TextView guessError = (TextView) findViewById(R.id.guessError);
TextView compGuess = (TextView) findViewById(R.id.tvCompGuess);

int guessValue = Integer.parseInt(guess.getText().toString());

if (guessValue > 20)
{
guessError.setVisibility(View.VISIBLE);
guess.getText().clear();
}
else if (guessValue < 1)
{
guessError.setVisibility(View.VISIBLE);
guess.getText().clear();
}
else
{
int min = 1;
int max = 20;

Random r = new Random();
int i = r.nextInt(max - min + 1) + min;

int computerNumber = i;
compGuess.setText(i);

if (computerNumber > guessValue)
{
guessError.setText("Too low!");
}
else if (computerNumber < guessValue)
{
guessError.setText("Too high!");
}
else if (computerNumber == guessValue)
{
guessError.setText("Good Guess!");
}
}

}




Aucun commentaire:

Enregistrer un commentaire