Java 错误的用户输入循环直到正确为止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39894744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:44:25  来源:igfitidea点击:

Java Wrong User Input Loop until correct

javaif-statementinputvalidating

提问by Julius'Web

Thanks for your time.

谢谢你的时间。

I have a problem validating the program. I have tried using, While/Switch but it is all the same. The issue is when a user enters the wrong input for example 5, it shows the error and then it lets them type it in again, but if they type in the wrong number again, the program does not validate... I can definitely copy the code again and again within it, but there should be an easier way.

我在验证程序时遇到问题。我试过使用,While/Switch 但它都是一样的。问题是当用户输入错误的输入时,例如 5,它会显示错误,然后让他们再次输入,但是如果他们再次输入错误的数字,程序不会验证......我绝对可以复制代码在其中一遍又一遍,但应该有更简单的方法。

Hope you understand what I am trying to achieve:D

希望你明白我想要达到的目标:D

How could I make it so it is a continues loop? Thank you!

我怎样才能使它成为一个继续循环?谢谢!

// Choosing the right room public static int rooms () {

// 选择合适的房间 public static int rooms() {

    int room;

    // Creating a new keyboard input
    Scanner scanner = new Scanner(System.in);
    // Displaying a message on the screen
    System.out.println("What room are you in? ");
    // Input
    room = scanner.nextInt();

    if (room==1) {

        roomOne();

    } else if (room==2) {

        roomTwo();

    } else if (room==3) {

        roomThree();

    } else if (room==4) {

        roomFour();

    } else {

        System.out.println("Wrong room number, please enter the room number.");
        room = scanner.nextInt();
    }


    //System.out.println("Sorry but you entered the wrong room number " + room + " Enter the correct room number 1-4 ");    


    return room;

} // End Rooms

回答by AxelH

You are looking for a while loop, something like this.

你正在寻找一个 while 循环,像这样。

I use a do ... while to execute the line at least once.

我使用 do ... while至少执行一次该行。

The methods check the value and print a message if this is not correct. Return false will prevent the code to exit the loop and read again.

如果这不正确,这些方法会检查该值并打印一条消息。返回 false 将阻止代码退出循环并再次读取。

{
     // Creating a new keyboard input
    Scanner scanner = new Scanner(System.in);


   int room;   
   do {
      // Displaying a message on the screen
      System.out.println("What room are you in? ");
      room = scanner.nextInt();
   } while( !isValid(room) );

   ... //if else or switch
}

private boolean isValid(int room){
   if(room > 4 || room  < 1){
       System.out.println("Try again ;)" );
       return false;
   }  else return true;
}

This is a quick code note even test.

这是一个快速的代码注释甚至测试。

回答by null

while (true) {
    int room = scanner.nextInt();
    if(room < 1 || room > 4) {
        System.out.println("Wrong room number, please enter the room number.");
        continue;
    }
    break;
}
if (room == 1) 
    roomOne();
else if (room == 2) 
    roomTwo();
else if (room == 3) 
    roomThree();
else if (room == 4) 
    roomFour();

Hope it helps, nevertheless you should read a little more about loops.

希望它有所帮助,不过你应该多读一点关于循环的内容。

回答by Alexander Skage

This is how you should set up a loop for input cleaning:

这是您应该如何设置用于输入清理的循环:

  1. Define a boolean value and assign a true or false value
  2. Make the while loop run on the boolean value
  3. When input is "clean", set the boolean value to true
  1. 定义一个布尔值并分配一个真值或假值
  2. 使 while 循环在布尔值上运行
  3. 当输入为“干净”时,将布尔值设置为 true