简体   繁体   English

在 do while 循环中正确使用 Scanner.next(Pattern pattern) 的问题

[英]Issues properly using Scanner.next(Pattern pattern) in do while loop

I have a menu where integers zero through six have different actions.我有一个菜单,其中整数零到六有不同的操作。 If one the user input is proper, it performs the action successfully and asks for input again for the next action to perform.如果用户输入正确,它会成功执行操作并再次要求输入以执行下一个操作。 To exit the program menu, the user input must be six.要退出程序菜单,用户输入必须为 6。 If any invalid input is received, it prints "invalid option" and reprints asks for input again.如果收到任何无效输入,它会打印“无效选项”并重新打印要求输入。 My code I have is:我的代码是:

int menuSelection;
Scanner reader = new Scanner(System.in);  // Reading from System.in

do
{
    System.out.println("Enter a number: ");
    if (reader.hasNext(Pattern.compile("^[0-6]$")))
    {
        menuSelection = reader.nextInt();
    }
    else
    {
        System.out.println("Invalid option");
        reader.next();
    }
} while(menuSelection != 6);
//once finished
reader.close();

The output I have is:我的输出是:

Enter a number: 
1
Enter a number: 
2
Invalid option
Enter a number: 
3
Invalid option
Enter a number: 
4
Invalid option
Enter a number: 
5
Invalid option
Enter a number: 
6
Invalid option
Enter a number: 
7
Invalid option
Enter a number: 
8
Invalid option
Enter a number: 
pancake
Invalid option
Enter a number: 

So it's properly handling the first ever input I give it, but after that, it's stuck in an infinite loop because it always thinks my input is incorrect.所以它正确地处理了我给它的第一个输入,但在那之后,它陷入了无限循环,因为它总是认为我的输入不正确。 My logic here was to have a do while that always repeats as long as menuSelection is not 6, because that's the only time the program will ever exit, everything else should loop back to waiting for input.我的逻辑是,只要menuSelection不是 6,就会一直重复执行一段时间,因为这是程序退出的唯一时间,其他所有内容都应该循环回等待输入。 So then inside the do while loop I was hoping I can error check there, making sure that the input is within the bounds of 0-6 and also strictly integer type, otherwise to go through the loop again without performing the action (which isn't actually coded into the if block yet).所以然后在 do while 循环中我希望我可以在那里进行错误检查,确保输入在 0-6 的范围内并且也是严格的整数类型,否则在不执行操作的情况下再次通过循环(这不是t 实际上已编码到 if 块中)。

I'm pretty new to Java, so I'm not the most familiar with Scanner and how it handles validation.我对 Java 还很陌生,所以我不是最熟悉 Scanner 以及它如何处理验证的人。 I guess my question is, what is causing my other inputs to be invalid?我想我的问题是,是什么导致我的其他输入无效?

Check your RegEx.检查您的正则表达式。 Try:尝试:

if (reader.hasNext(Pattern.compile("[0-6]"))) {
    menuSelection = reader.nextInt();
}

This means, when the pattern [0-6] ( any number between 0 and 6 ) was detected then it works as you expected it to.这意味着,当检测到模式[0-6]0 和 6 之间的任何数字)时,它会按您的预期工作。

[edit] [编辑]

I removed the {1} ( only one time ) at the end, as cricket_007 proposed.正如cricket_007 所提议的那样,我最后删除了{1}仅一次)。

[/edit] [/编辑]

Example:例子:

Enter a number: 
sdf
Invalid option
Enter a number: 
3
Enter a number: 
2
Enter a number: 
1
Enter a number: 
5
Enter a number: 
8
Invalid option
Enter a number: 
6

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

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