我正在使用while循环和if循环来确定响应和操作。由于某些奇怪的原因,它继续忽略我的if语句。Java while循环问题
Boolean _exit = false;
while (_exit == false){
System.out.println("\nWould you like another meal?\tYes or No?");
String answer = scan.next().toLowerCase();
if (answer == "yes"){
System.out.println("Reached1");
_exit = true;
}
if (answer == "no"){
System.out.println("Reached1");
exit = _exit = true;
}
有人可以解释发生了什么,为什么它没有检查if语句。我也试过scan.nextLine。当我移除toLowerCase时,这个问题甚至持续存在,因为它引起了我的注意,它可能对字符串值产生影响,尽管我尝试了Locale.English。
有什么建议吗?
不要拿'String'值与''==;与“equals”方法比较。 – rgettman
顺便说一下,在第二个if条件中'exit'是什么,它应该是一个'else if'。 –