我有一个 while 循环,其循环条件是 boolean 变量为 true。循环中包含在退出循环时将 boolean 变量设置为 false 的逻辑。然而,它永远不会存在。我已通过打印一条消息确认该变量已设置为 false。
以下是该方法的概要:
public void collapse(){
boolean status=true;
while(status=true){
// some processing
if (some condition) {
System.out.println("Setting status to false");
status = false;
}
// more processing that doesn't set status to true
}
}
这是怎么回事?
最佳答案
您在 while 语句中将状态分配为 true,而不是检查相等性。要么将其更改为 status == true,要么您可以只使用 status。
关于Java 方法 while 循环无休止,即使似乎满足了突破情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856276/