Search code examples
javainputwhile-loopdialogprompt

Re-prompt Dialog input until guessing number is met -java


I been trying to prompt the same input dialog, until the user guess the correct number, but I'm not sure how, any help would be appreciate! I'm pretty sure is how to implement the while loop but I don't know how exactly. The user input is a string parsed to int for the calculations

JOptionPane.showMessageDialog(null, "\nWelcome to Guess-       My-Number\nA GAME of CHANCE and SKILL\nLet's Play!\n\n");

            // input number
         String myGuess = JOptionPane.showInputDialog("Enter a number between 1 and 25:");
         int myGuess_int = Integer.parseInt(myGuess);

         final int NUMBER_TO_GUESS = 13;



           //good or bad guess??
         while(myGuess_int != NUMBER_TO_GUESS)
         {
            if(myGuess_int < NUMBER_TO_GUESS) //too low
            {
               String message = String.format("Your guess [ %s ] is too low...\nTry Again!\n", myGuess);
               JOptionPane.showMessageDialog(null, message);   
            } 
            else //too high
            {
               String message = String.format("Your guess [ %s ] is too high...\nTry Again!\n", myGuess);
               JOptionPane.showMessageDialog(null, message);             
            }

            JOptionPane.showInputDialog("Enter a number between 1 and 25:");
            myGuess_int = Integer.parseInt(myGuess);    

          }//end while


          String message = String.format("Your guess [ %s ] is the number...\nCongratulations!\n", myGuess);
          JOptionPane.showMessageDialog(null, message);


      } //end main

   } //end class

Solution

  • Try using a do-while loop!

                JOptionPane.showMessageDialog(null, "\nWelcome to Guess-       My-Number\nA GAME of CHANCE and SKILL\nLet's Play!\n\n");
    
             final int NUMBER_TO_GUESS = 13;
    
             //good or bad guess??
             String myGuess;
             int myGuess_int;
             do{
                // input number
                myGuess = JOptionPane.showInputDialog("Enter a number between 1 and 25:");
                myGuess_int = Integer.parseInt(myGuess);
                if(myGuess_int < NUMBER_TO_GUESS) //too low
                {
                   String message = String.format("Your guess [ %s ] is too low...\nTry Again!\n", myGuess);
                   JOptionPane.showMessageDialog(null, message);   
                } 
                else //too high
                {
                   String message = String.format("Your guess [ %s ] is too high...\nTry Again!\n", myGuess);
                   JOptionPane.showMessageDialog(null, message);             
                }
    
                JOptionPane.showInputDialog("Enter a number between 1 and 25:");
                myGuess_int = Integer.parseInt(myGuess);    
    
              }while(myGuess_int != NUMBER_TO_GUESS);//end while
    
    
              String message = String.format("Your guess [ %s ] is the number...\nCongratulations!\n", myGuess);
              JOptionPane.showMessageDialog(null, message);
    
    
          } //end main
    
       } //end class