You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中LogicGate类not方法修改布尔变量异常:无法二次翻转值

问题分析与解决方案

嘿,我一眼就瞅出问题所在了!你的not方法里的条件判断犯了个新手常踩的小坑——把比较运算符==写成了赋值运算符=,这直接导致第二次调用not方法时,永远没法把状态从false改回true。

具体问题拆解

看你not方法里的这段代码:

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

这里的currentState = true不是在判断当前状态是否为true,而是把true赋值给了currentState,所以这个if条件永远会成立,每次调用not都会执行negate方法把状态设为false。哪怕第二次调用时原本状态是false,赋值后currentState变成true,条件依旧成立,还是会走negate分支,自然看不到期望的true结果。

修正后的代码

修复核心问题后的LogicGate类

public class LogicGate {
    private boolean state;

    public LogicGate(boolean logicGateState) {
        state = logicGateState;
    }

    public void not(LogicGate obj) {
        boolean currentState = obj.state();
        System.out.println("....." + currentState);
        // 把赋值=改成比较==
        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");
        }
    }

    public void set(LogicGate obj) {
        obj.setState(true);
    }

    public void negate(LogicGate obj) {
        obj.setState(false);
    }

    public boolean state() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }
}

主类代码(无需修改)

public 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);
    }
}

额外优化建议

其实你的not、set、negate方法完全不需要传参,因为它们都是实例方法,可以直接操作当前对象的state属性,这样代码会更简洁易读:

public class LogicGate {
    private boolean state;

    public LogicGate(boolean logicGateState) {
        state = logicGateState;
    }

    public void not() {
        System.out.println("....." + state);
        if(state == true) {
            negate();
            System.out.println("The state has changed to: " + state + " Should be false");
        } else {
            set();
            System.out.println("The state has changed to: " + state + " Should be true");
        }
    }

    public void set() {
        this.state = true;
    }

    public void negate() {
        this.state = false;
    }

    public boolean state() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }
}

对应的主类调用也改成:

logicGate.not();

这样是不是清爽多啦?

内容的提问来源于stack exchange,提问作者Liam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:05:11