我正在运行一个循环,当它到达else语句时,它应该停止运行。当我调试时,布尔电源会更新,但仍会进入循环。我知道我可以使用System.Exit(0); 或休息;但是我会理解为什么它会在错误条件下继续运行的逻辑?
public class Mmu {
//code omitted
public static final Mmu MMU = new Mmu();
public static void main(String[] args) {
MMU.runProcesses();
//code omitted
}
private Mmu() {
//code omitted
}
protected void runProcesses(){
boolean power= false; // running processes, normally this would start as false but I changed to test
boolean twoFinished = false;
boolean oneFinished = false;
while (power = true) { //still entering this when power = false why
twoFinished = MMU.processTwo.finished();
oneFinished = MMU.processOne.finished();
if (oneFinished = false) {
MMU.processOne.thread();
} else if (twoFinished = false) {
MMU.processTwo.thread();
} else {
power = false;
System.out.println("All processes Finished");
//System.exit(0);
}
}
}
}
感谢您提前提出任何建议。
while (power = true)
是否总是 true
因为你分配和不进行比较。
写:
while(power)
反而。
分配的表达式返回分配的值。
这就是为什么我们不喜欢==
在比较boolean
s时使用它,这可能会导致这种错误。您可以直接写if(someBoolean)
而不是if(someBoolean == true)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句