0

So I'm really new to java and I was trying to practice using while and if statements through a guessing game app.

Everything seemed to work until I'm promped to play again. When I type Y, the loop ends. This isn't supposed to happen as the while argument at the beginning of the code says to keepPlaying if true.

I've tried playing with the argument to make it: while (answer == "Y"). I've played around with it but it always keeps exiting. Please help!!

Here's the code:

import java.util.Scanner;
public class GuessingGame 
{
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        String answer = "Y";
        int guess;
        int number;
        String again;
        boolean keepPlaying = true;


        System.out.println("Let's play a guessing game!");
         while (keepPlaying)
        {
            System.out.println("I'm thinking of a number between 1 and 10.");
            System.out.print("What do you think it is? ");
            guess = sc.nextInt();
            while (guess > 10 || guess < 1)
            {
                System.out.print("I said, between 1 and 10. Try again: ");
                guess = sc.nextInt();
            }
            number = (int)(Math.random() *10 + 1);
            if (guess == number)
            {
                System.out.println("You're right!");

            }
            else
            {
                System.out.println("You're wrong! the number was " + number);

            }
            System.out.print("Play again? (Y or N)");
            answer = sc.next();

            if (answer == "Y")
            {
                keepPlaying = true;
            }
            else
            {
                break;
            }

        }
        System.out.println("Thank you for playing");
    }

}
Mike95
  • 11
  • 1

1 Answers1

0

In the comparison, you should compare using the String requirements:

if (answer.equals("Y"))   // NOTE: I'd use .equalsIgnoreCase(...)
        {
            keepPlaying = true;
        }
        else
        {
            break;
        }

Also note that since this comparison is at the end of the loop, you could simplify to:

keepPlaying = answer.equalsIgnoreCase("Y");

As you do not need the break statement since the loop will be re-evaluated.

KevinO
  • 4,111
  • 4
  • 23
  • 34
  • Hey Kevin, thanks so much that really worked. I'm surprised how a simple tweak solved this problem. I'm still confused as to how: answer == "Y" didn't work but answer.equals("Y") works. Intuitively aren't they the same? – Mike95 May 14 '17 at 03:43
  • @Mike95, glad the answer helped. If appropriate, please accept the answer. As to why the `==` didn't work, it is because `==`, for objects, tests for *reference equality* (that is, are the objects the same object), whereas the `.equals()` invokes the method to check for equality. For a `String` object, the `.equals()` method checks to see if the characters are the same. There is additional information at [How Do I Compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?noredirect=1&lq=1). – KevinO May 14 '17 at 04:54