-6

I'm making a Random Number guessing app, but the app only works once, and you have to relaunch the app to play again, how do i make users to play again after they have guessed the right result by changing the random number again. And also, i would like the app to detect if users had inputted a wrong number or strings inside the text field, how can i do that? Any help will be appreciated, Thanks!

    int randomNumber;

public void output(View view)
{
    EditText guess = (EditText) findViewById(R.id.editText);
    String guessedNumberString = guess.getText().toString();

    int guessedNumberInt = Integer.parseInt(guessedNumberString);

    String message = "";
    try {
        if (guessedNumberInt > randomNumber) {
            message = "Too high";
        } else if (guessedNumberInt < randomNumber) {
            message = "Too low";
        } else {
            message = "You are right! Try it again!";
        }
    }
    catch (InputMismatchException e)
    {
        message = "Enter a valid number";
    }
    Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random randomGenerator = new Random();
    randomNumber = randomGenerator.nextInt(21);
}
Kevin You
  • 84
  • 10
  • InputMismatchException is not what you think it is – njzk2 Dec 16 '15 at 04:54
  • Possible duplicate of [Cannot Make Static Reference to Non-Static Method](http://stackoverflow.com/questions/4969171/cannot-make-static-reference-to-non-static-method) – Mr Robot Dec 16 '15 at 05:42

3 Answers3

0

There are two ways i know of: 1. create a startGame method to setup the game i.e. generate random and in your output method call it after user guessed it 2. use this to refresh whole activity finish(); startActivity(getIntent());

And for 2nd part play around with some if else before converting to integer

Alok Subedi
  • 1,601
  • 14
  • 26
0

Here's the logic:

 if(input == randNum){


ReLaunchGame();

}

Now, for method ReLaunchGame() you must either restart the activity, or reset all the textfields.

Just compare their input with the random number you picked. Now, to see if they entered a wrong input. You must first understand exception handling, which I think you do.

enter image description here

(http://www.mathcs.emory.edu/)

Scanner sc=new Scanner(System.in);
try
{
  System.out.println("Please input an integer");
  //nextInt will throw InputMismatchException
  //if the next token does not match the Integer
  //regular expression, or is out of range
  int usrInput=sc.nextInt();
}
catch(InputMismatchException exception)
{
  //Print "This is not an integer"
  //when user put other than integer
  System.out.println("This is not an integer");
}

Thats it. Let me know if it worked! :-)

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
0

To check if the user inputted a number, put the parseInt() line inside the try block. The exception you have to catch here is NumberFormatException. So it should look like this:

try {
    int guessedNumberInt = Integer.parseInt(guessedNumberString);
    ...
}
catch (NumberFormatException e) {
    message = "Enter a valid number";
}

I am not familiar with Android, so I don't know when that output() function is called. However, you could try creating a flag before the try block to indicate if the user guessed the number correctly. Then, create another random number at the end of the function if the flag is true. You would also need to make the randomGenerator a class variable.

Random randomGenerator = new Random();
public void output(View view)
{
    ...
    boolean correct = false;
    try {
        ...
        } else {
            message = "You are right! Try it again!";
            correct = true;
        }
    }
    ...
    Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
    if (correct) {
        randomNumber = randomGenerator.nextInt(21);
    }
}
Gerardo Figueroa
  • 549
  • 2
  • 6
  • 16