-1

So I've been using java for a few weeks now and wanted to try to make my first little game. Everything I had was working fine until I tried adding a play again feature. It looks like it just skipping a small portion of code at the bottom and I can't figure out why.

I've tried moving different parts of the code around but can't figure out why it's not working.

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.println("You have 10 tries to correctly guess a random number between 1 and 10. Good luck! \n"
            + "After you have entered a number, press ENTER to see if you won.");

    boolean play;
       play = true;

   do 
   {     
       int tries = 10, triesLeft = 0, triesUsed = 0;

       String YorN; 
    do
    {

        System.out.print("\nEnter A Number: ");

        int numInp = scan.nextInt();    
        int randomNumber = (int)(Math.random()*9) + 1;

        System.out.println("The number was " + randomNumber);

        if(numInp == randomNumber) {
            System.out.println("\nYou Win! Great job.");
            triesLeft = --tries;
            triesUsed = 10 - triesLeft;
            tries = -1;
        } else {
            System.out.println("Try Again...");
            tries --;
        }
    }
    while( tries > 0);

    String playAgain = (" Feel free to play again soon!");

    if(tries == 0) {
        System.out.println("\nOut of tries :(   You lose loser  " + playAgain);
    }
    if(tries == -1) {
    System.out.println("\nYou used " + triesUsed + " tries and had "
            + triesLeft + " tries left! :)" + playAgain);
    }

    System.out.print("\nDo you want to play again? Y or N?");

    YorN = scan.nextLine();

    if (YorN == ("Y") || YorN == ("y"))
    {
        play = true;
    }
    else if (YorN == ("N") || YorN == ("n"))
    {
        play = false;
    }
    else {}

   } while(play = true);

   if (play = false){
       System.out.println("\nGood Bye"); 
   }

}

This system doesn't seem to be doing anything when I run the code:

YorN = scan.nextLine();

    if (YorN == ("Y") || YorN == ("y"))
    {
        play = true;
    }
    else if (YorN == ("N") || YorN == ("n"))
    {
        play = false;
    }
    else {}

Here's what I see when I run it:

You Win! Great job.

You used 5 tries and had 5 tries left! :) Feel free to play again soon!

Do you want to play again? Y or N? Enter A Number:

It doesn't give me a spot to type y or n as it should. If anyone knows why I would really appreciate the help

phflack
  • 2,689
  • 1
  • 7
  • 21
  • 2
    You have multiple common errors in your code, such as using `nextLine` after `nextInt`, comparing strings with `==` and comparing values with `=`. – Sweeper Sep 29 '19 at 10:15
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Tom Sep 29 '19 at 10:31
  • And the other dupe: [Scanner is skipping nextLine() after using next() or nextFoo()?](//stackoverflow.com/q/13102045) – Tom Sep 29 '19 at 10:32
  • Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – SSP Sep 29 '19 at 11:33

1 Answers1

0

nextInt() or next() method does not read enter. usually after number you must be pressing enter which is not readied by above method. a sort cut to avoid this is keep 1 extra sc.nextLine() method call. it will read and discard enter.

scan.nextLine();
YorN = scan.nextLine();

    if (YorN == ("Y") || YorN == ("y"))
    {
        play = true;
    }
    else if (YorN == ("N") || YorN == ("n"))
    {
        play = false;
    }
    else {}
SSP
  • 2,703
  • 5
  • 27
  • 47
  • Thanks for the advice it made my code almost where I want it to be. With the advice you gave as well as one other thing I read, I got my code to a position where when you finish and type 'y' or 'n' it works perfectly, my only problem is when I just click enter, it restarts the code anyways. If you know why it is doing this or have a solution it would be very much appreciated. Also, would it be more beneficial to change YorN from a String to a char? Thanks, Frank – FrankJaffe Oct 01 '19 at 18:32