I can't figure out how to restart a loop, when the person enters in a wrong input

Related searches

Ok so for an assignment I need to make a loop for the user to input a number 1-4 for either rock paper scissors or to end the game, but if they enter in anything other than those I need them to be able to re-enter the number but I can't seem to get my loop to restart or something similar...

Code (It's just my while loop

while(user < 5)
          {
              System.out.println("Please enter in a number");
              computer = generator.nextInt(3) + 1;            
              user = keyboardIn.nextInt();

              //tell the player what was chosen
              if(user == 1)//player is rock
              {
                 System.out.println ("Player is rock");  
              }
              else if (user == 2)//player is paper
              {
                 System.out.println ("Player is paper");
              }
              else if (user == 3) //player is scissors
              {
                System.out.println ("Player is scissors");

              }
              else if (user == 4)
              {
                  System.out.println("Thank you for playing!");
                  break;
              }
              else if (user >= 5)
              {
                  System.out.println(user + " was never an option. \nTry again.");
                  user = keyboardIn.nextInt();
              }

              //tell the player what the computer has chosen
              if(computer == 1)//computer is rock
              {
                 System.out.println ("Computer is rock");
              }
              else if (computer == 2)//computer is paper
              {
                 System.out.println ("Computer is paper");
              }
              else if (computer == 3)//computer is scissors
              {
                 System.out.println ("Computer is scissors");
              }


              //determine winner
              if (user == computer) //tie
              {
                 System.out.println("It is a tie");
              }
              else if (user < computer) //player is rock
              {
                 System.out.println("Computer wins");
              }
              else //computer is scissors
              {
                 System.out.println("Player wins");
              }
              System.out.println();
          }

You can use continue to jump back to the top of the loop. Note, however, that at the top you ask for user input, and on error you do so too; that's one time too many.

I Can (Nas song), Informed Consent Action Network ICAN is a non profit organization focused on the scientific integrity of vaccines and pharmaceutical Industry.

You could use

boolean gameOver = false;
while(!gameOver){
    if (user < 5){
     ....
    }
}

and set gameOver = true in each of your game-ending conditions.

Informed Consent Action Network: Home, ICAN Torrance. We are currently building a permanent location in Torrance. After years of operating in temporary locations ICAN is excited to build roots in Torrance and serve even more people with disabilities throughout the South Bay.

Replace the part:

else if (user >= 5)
              {
                  System.out.println(user + " was never an option. \nTry again.");
                  user = keyboardIn.nextInt();
              }

With:

else if (user >= 5)
              {
                  System.out.println(user + " was never an option. \nTry again.");
                  continue;
              }

Basically the continue, brakes the current loop and start over a new one, of course verifying first the condition...

However if you want your code to be a little bit more efficient, in your specific case this would be my way of doing it:

while(true) {
    computer = generator.nextInt(3) + 1; 
    do {
        System.out.println("Please enter in a number");           
        user = keyboardIn.nextInt();
    } while(user>=5 && user<=0);
    //The rest of your code deleting also the last else if statement
 }

This way your code is cleaner and more maintainable. Plus you will be sure that after the code exit from the loop you will definitely have a number between 0 and 4 which would be a valid option

I☆Can is the story of a girl (Miyu) who is just starting out as an idol. Her manager (Ryouta) is also her high-school sweetheart. They are in a slowly progressing, supportive relationship, when Miyu's producer (Terada) decides he wants a piece of her.

It has been about 3 and ½ years since Noah received his diagnosis. He is now 7 years old, and is in 1st grade at the local public school. He had initially received services through the public school system, but since earlier this year, he did not meet the criteria for those services.

Contact us to get started on the journey. We are committed to the best hope and future for your child and family. We have the the essential services you need.

The I CAN Enquiry Service continues to operate on Mondays to Wednesdays between 9.30 - 12.30 and 1.30 - 4.30. Please leave voicemail if you cannot get through and we will endeavour to call you back during opening hours. To have a chat with an I CAN speech and language therapist, call 020 7843 2544

Comments
  • code before will be useful