title: Java Guessing Game infinite loop
body: <p>I'm having an issue with my code where the program will not play again, but 
  also will not end. </p>
  
  <p>The code continues to loop in the inner while loop after a win or too many guesses.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>/**
   * This program will prompt the user to guess a secret number
   * This number will is between 1 and N, where N is a postive number
   *
   * @author: Perry Chapman
  */
  
  import java.util.Scanner;
  
  public class SecretNumber2
  {
      public static void main( String [] args )
      {
          int N;
          int randomNumber;    
          int guess;
          int tries, maxTries;
  
          boolean win = false;
          boolean playing = true;
          int playAgain = 1;
          tries = 0;
  
          while(playing == true)
          { 
              Scanner input = new Scanner( System.in );
  
              System.out.println( "This is a guessing game." );
              System.out.println( "What is the max number of tries: ");
              maxTries = input.nextInt();
              System.out.println( "Enter a value between 1 and 1000: " );
              N = input.nextInt();
  
              randomNumber = (int)(N * Math.random()) + 1;
  
              while (win == false)
              {
                  System.out.println( "Enter your guess: " );                      
                  guess = input.nextInt();
                  tries++;
  
                  if (guess == randomNumber) 
                  {  
                      System.out.println( "Congratulations! The number of guesses it took you was " + tries );
                      win = true;
                      System.out.println("Would you like to play again? \n (1)Yes or (2)No: ");
                      playAgain = input.nextInt();
  
                      if(playAgain == 1) {
                          playing = true;
                      }
  
                      else if (playAgain == 2) {
                          playing = false;
                      }
                      tries = 0;
                  }
  
                  else if(guess &lt; randomNumber)
                      System.out.println( "Too low, guess again." );
  
                  else if(guess &gt; randomNumber)                        
                      System.out.println( "Too high, guess again." );
  
                  else if(tries == maxTries)
                  {
                      System.out.println("You have exceeded the max number of tries. \nYou lose.");
                      System.out.println("\nWould you like to play again?\n (1)Yes or (2)No: ");
                      playAgain = input.nextInt();
  
                      if(playAgain == 1) {
                        playing = true;
                      }
  
                      else if(playAgain == 2) {
                        playing = false;
                      }
  
                      tries = 0;
                  }
              }
          }
      }
  }
  </code></pre>