我有一個循環運行,當它到達else語句時它應該停止運行。當我調試布爾功率更新但它仍然進入循環。我知道我可以使用System.Exit(0);或休息;但我會理解爲什麼它會繼續以假條件運行的邏輯?爲什麼while循環在條件= false時不退出
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);
}
}
}
}
感謝您的任何建議提前。
'power = true'是賦值,而不是比較;)。你需要'power == true',或者'while(power)'。 – theGreenCabbage 2014-10-28 07:37:06
'='是賦值運算符,'=='是比較運算符 – 2014-10-28 07:37:12
'while(power == true)'note double == == – 2014-10-28 07:37:12