因此,我已经连接了调试器,并尝试了不同的输入,但我似乎无法弄清为什么它不会通过循环。运行时,我输入“ l”或“ L”,然后将输入设置为该值,然后将输入设置为大写版本,然后重复该操作。
public static char displayMenu(){
char input;
sc.nextLine();//clear junk
do {
System.out.println();
System.out.println("\t\t Enter L to (L)oad ");
String entry = sc.nextLine();
input = entry.toUpperCase().charAt(0);
} while (input != 'L' || input!='M' || input != 'P' || input != 'Q');
您boolean
||
不正确。如果L
它是一个值,则不是M
,P
否则Q
您的循环将继续进行迭代。我想你想要类似的东西,
while (input != 'L' && input != 'M' && input != 'P' && input != 'Q');
或者
while (!(input == 'L' || input == 'M' || input == 'P' || input == 'Q'));
考虑何时input
是L
,显然L
不是M
,因此您的初始while
状况将继续迭代。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句