mercredi 24 février 2016

Change Random() int value on android

package com.androidapps.numberguesser;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

int randomNumber;

public void randomGenerator(View view) {

    EditText guess = (EditText) findViewById(R.id.guessText);
    String myGuess = guess.getText().toString();

    int guessInt = new Integer(guess.getText().toString());

    if (guessInt == randomNumber) {

        Toast.makeText(getApplicationContext(), "You Guessed The Right Number!!! " + "( " + guessInt + " )" , Toast.LENGTH_LONG).show();
        guess.setText("");

    } else if (guessInt > randomNumber) {

        Toast.makeText(getApplicationContext(), "Your Guess Is Too High! " + "( " + guessInt + " )", Toast.LENGTH_SHORT).show();
        guess.setText("");


    } else {

        Toast.makeText(getApplicationContext() , "Your Guess Is Too Low! " + "( " + guessInt + " )", Toast.LENGTH_SHORT).show();
        guess.setText("");


    }

}

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

    Random randNum = new Random();
    int randomNumber = randNum.nextInt(21);

}
    }

I am trying to change the value of 'randomNumber' when the user guesses the right number to a different random value but I have not found the solution. I have tried to use a boolean but that did not work as it came up with an error "Variable 'randNum' is already defined in the scope"

Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire