难以忽略for while循环时的区分大小写

时间:2015-02-28 23:04:08

标签: java loops case

尝试让用户使用字母“N”或“n”退出程序,但只有在使用“N”时才会退出程序

以下是我的代码,感谢任何帮助!!

        System.out.print(" Do you want to repeat the ");
        System.out.println("program ['Y' or 'N']");
        Choice = sc.next().charAt(0);

    } while (Choice != 'N' || Choice != 'n');
}
}

2 个答案:

答案 0 :(得分:0)

您必须使用while (Choice != 'N' && Choice != 'n');。 它必须是AND条件。不是OR条件。

答案 1 :(得分:0)

您使用了错误的运算符:

while (Choice != 'N' && Choice != 'n');

现在,您的代码声明当字符不是'N'或不是'n'时循环将继续。由于字符不能同时为“N”和“n”,因此循环将始终继续。将运营商切换为&&要求该字符 Nn

相关问题