亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用類方法更改布爾變量的值?

使用類方法更改布爾變量的值?

開心每一天1111 2023-09-13 10:08:37
所以我正在擺弄java,并遇到了我試圖編寫的一些代碼的問題。我創建了一個類,LogicGate在該類中調用有方法將實例的狀態設置LogicGate為 true 或 false。LogicGate只有一個屬性及其布爾值 true 或 false。所有方法似乎都有效,但只有一個(稱為 not()),我將布爾值更改為 false(如果 true)和 true(如果 false)。該代碼第一次將值從 true 更改為 false,但第二次它似乎忽略了 if 語句。//Mainpublic class main {    public static void main(String[] args) {        LogicGate logicGate = new LogicGate(true);        System.out.println(logicGate.state());        System.out.println("");        logicGate.not(logicGate);        System.out.println("");        System.out.println(logicGate.state());        System.out.println("");        logicGate.not(logicGate);    }}//Class Logicgatepublic class LogicGate {    //The attribute for the for the object    private boolean state;    //Creating the constructor for the object logic gate     public LogicGate(boolean logicGateState) {        state = logicGateState;    }    //This method changes the state of the Logic Gate    public  void not(LogicGate obj) {        //Saving the current state of the logic gate        boolean currentState = obj.state();        System.out.println("....." + currentState);        //Checks the state of the object instance and changes it accordingly        if(currentState = true) {            obj.negate(obj);            System.out.println("The state has changed to: " + obj.state() + " Should be false");        }        else if(currentState = false) {                 obj.set(obj);            System.out.println("The state has changed to: " + obj.state() + " Should be true");        }    }    //Sets the state of the object boolean variable to true     public  void set(LogicGate obj) {        obj.setState(true);    }    //Sets the state of the object boolean variable to false     public  void negate(LogicGate obj) {         obj.setState(false);    }    //This method will return the state of the object    //it can be either true or false     public boolean state() {        return state;    }每次調用 not 方法時,布爾值都應該改變。代碼的最后一行應該說The state has changed to: true Should be true。
查看完整描述

2 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

所以有幾個問題,即:

  1. 運算符=是賦值運算符。您正在進行比較,這意味著您需要使用==運算符。

  2. 對于boolean值,您實際上不需要在 if 中使用比較運算符,因為它是trueor false(不是 true)。

  3. 如果你的第一個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;

}


查看完整回答
反對 回復 2023-09-13
?
楊魅力

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");

告訴我是否有效


查看完整回答
反對 回復 2023-09-13
  • 2 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號