简体   繁体   English

Java While-Loop程序

[英]Java While-Loop Program

I am trying to get a valid input of "y", "Y", "n" , or "N". 我正在尝试获取“ y”,“ Y”,“ n”或“ N”的有效输入。

If the input is not valid (for example any word that starts with a "y" or "n") I want it to re-prompt the user for input. 如果输入无效(例如,任何以“ y”或“ n”开头的单词),我希望它重新提示用户输入。

So far I have: 到目前为止,我有:

while  (again.charAt(0) != 'N' && again.charAt(0) != 'n' && again.charAt(0) !='Y' && again.charAt(0) != 'y' ) {
    System.out.println ("Invalid Inpur! Enter Y/N");
    again = numscan.next();
}

if (again.charAt(0)== 'N' || again.charAt(0) == 'n') {
    active = false;                   
} else {
    if (again.charAt(0)== 'Y' || again.charAt(0) == 'y'){
        active = true;
        random = (int) (Math.random () *(11));
    }
}

The problem I am having is if I enter any word that starts with the letter "y" or "n" it senses it as valid input (since it is the character at slot 0). 我遇到的问题是,如果我输入以字母“ y”或“ n”开头的任何单词,它将其视为有效输入(因为它是插槽0中的字符)。 I need help fixing this so I can re-prompt the user when they enter a word that starts with a "y" or "n". 我需要帮助解决此问题,以便在用户输入以“ y”或“ n”开头的单词时再次提示他们。

Thanks! 谢谢!

You could just test to make sure the length of the input is 1: 可以进行测试以确保输入的长度为1:

again.length() == 1

But a better approach might be: 但是更好的方法可能是:

while (! (again.equalsIgnoreCase("n") || again.equalsIgnoreCase("y"))) {
    ...
}

or even 甚至

while (! again.matches("[nyNY]")) {
    ...
}

Assuming again is a string containing the complete user input, you could use: again假设是一个包含完整用户输入的字符串,则可以使用:

while (!again.equals("N") && !again.equals("n") ...

The .equals() method will match only if the entire string matches. .equals()方法仅在整个字符串匹配时才匹配。

One of the way would be: 一种方式是:

First check again String length is only ONE character. again首先检查字符串length只有一个字符。 If not, ask again. 如果不是,请再次询问。

if(again.length() ==1)
{
 while  (again.charAt(0) != 'N' && again.charAt(0) != 'n' && again.charAt(0) !='Y' && again.charAt(0) != 'y' ) {
               System.out.println ("Invalid Inpur! Enter Y/N");
                again = numscan.next();
            }
.....

}else
     {
System.out.println ("Invalid Inpur! Enter Y/N");
                    again = numscan.next();
    }

It sounds like what you want is: 听起来您想要的是:

while  (!again.equals("N") && !again.equals("n") && !again.equals("Y") && !again.equals("y") ) {
    System.out.println ("Invalid Inpur! Enter Y/N");
    again = numscan.next();
}

This way you can also easily add Yes/No, etc later if you want. 这样,您以后也可以根据需要轻松添加Yes / No,等等。

Regex could be an alternative to have strict input checks. 正则表达式可能是进行严格输入检查的替代方法。 Following piece of code validates y or n ignoring the case. 以下代码验证y或n忽略大小写。

while (!again.matches("(?i)^[yn]$")){
    System.out.println ("Invalid Inpur! Enter Y/N");
    again = numscan.next();
}
active = (again.equalsIgnoreCase("Y"))? true : false;

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

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