Performing work on the principle of Y / N

I need help with the SIMPLE Y / N Condition for my program. I really don't work the way I want.

Unfortunately, all the other topics that I find are very confusing. I am very new to programming.

I want a Y / N condition that will not work and will not be CASE SENSITIVE. therefore, if Y or y returns to another menu, if n and N just stop the program, and if something else types in it, it will loop until the conditions Y or N. are met.

Here is what I wrote:

String input = ScanString.nextLine();

while (!"Y".equals(input) || !"y".equals(input) || !"N".equals(input) || !"n".equals(input)) {
    System.out.println("Please enter Y/N (Not case sensitive): ");
    input = ScanString.nextLine();
}

if ("Y".equals(input) || "y".equals(input)) {
    meny1();
} else if ("N".equals(input) || "n".equals(input)) {

}

When it starts, no matter what I put in, it will not break the while loop.

+4
source share
2 answers

while (!"Y".equals(input) || !"y".equals(input) ||... " , " Y " , " y " ...". , .

, , - (&&), :

while (!input.equalsIgnoreCase("Y") && !input.equalsIgnoreCase("N")) { 

, " , " Y "" y " , " N "" n ".

Yoda-talk, Yoda-speak:

while (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) { 
+8

while (!("Y".equalsIgnoreCase(input)) && !("N".equalsIgnoreCase(input))) {

}

String[] validInputs = { "Y", "N" };
while(!Arrays.asList(validInputs).contains(input.toUpperCase())) {

}
0

All Articles