简体   繁体   English

当这个人输入错误的输入时,我不知道如何重新启动循环

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

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...好的,所以对于作业,我需要为用户创建一个循环,以便为剪刀石头布输入数字 1-4 或结束游戏,但是如果他们输入的不是我需要的任何内容,我需要他们能够重新-输入数字,但我似乎无法让我的循环重新启动或类似的东西......

Code (It's just my while loop代码(这只是我的 while 循环

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.您可以使用continue跳回到循环的顶部。 Note, however, that at the top you ask for user input, and on error you do so too;但是请注意,在顶部您要求用户输入,如果出错,您也会这样做; that's one time too many.这是一次太多。

You could use你可以用

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

and set gameOver = true in each of your game-ending conditions.并在每个游戏结束条件中设置 gameOver = true。

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另外,您将确保在代码退出循环后,您肯定会有一个介于 0 和 4 之间的数字,这将是一个有效的选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM