2 回答

TA貢獻1859條經驗 獲得超6個贊
所以有幾個問題,即:
運算符=是賦值運算符。您正在進行比較,這意味著您需要使用==運算符。
對于
boolean
值,您實際上不需要在 if 中使用比較運算符,因為它是true
orfalse
(不是 true)。如果你的第一個
if
正在檢查 atrue
那么你的 else 邏輯上正在檢查false
所以你的代碼應該看起來像這樣:
if(currentState) {
obj.negate(obj);
System.out.println("The state has changed to: " + obj.state() + " Should be false");
} else {
obj.set(obj);
System.out.println("The state has changed to: " + obj.state() + " Should be true");
}
然而,我也不明白為什么您將LogicGate對象傳遞到 的方法中LogicGate,以設置其中包含的變量的狀態LogicGate(或任何與LogicGates此相關的方法)。為什么不使用已有的方法setState(boolean state),那么你的實現將是:
if(currentState) {
obj.setState(Boolean.FALSE);
System.out.println("The state has changed to: " + obj.state() + " Should be false");
} else {
obj.setState(Boolean.TRUE);
System.out.println("The state has changed to: " + obj.state() + " Should be true");
}
但最終,您的not()方法應該像這樣簡單:
public void not() {
this.state = !state;
}

TA貢獻1811條經驗 獲得超6個贊
嘗試將所有單個 = 更改為“==”或析構函數代碼
if(currentState) {
obj.negate(obj);
System.out.println("The state has changed to: " + obj.state() + " Should be false");
}
else{
obj.set(obj);
System.out.println("The state has changed to: " + obj.state() + " Should be true");
告訴我是否有效
添加回答
舉報