Counter:reduce方法忽略boolean

时间:2013-12-02 14:04:21

标签: java boolean

我正在尝试创建一个包含可以增加和减少的数字的计数器。该程序还有一个布尔检查:当为真时,计数器不能为负数。该程序似乎运行正常,但我不能得到减少方法(减少一个减少输入)以获得布尔权。它不检查布尔值或什么?我是Java新手,需要帮助理解错误。课程如下:

public class Counter {

    private int value;
    private boolean check;

    public Counter(int startingValue, boolean check) {
        if (this.check = true) {
            this.value = startingValue;
            if (value < 0) {
                value = 0;
            }
        }
        if (this.check = false) {
            this.value = startingValue;
        }

    }

    public Counter(int startingValue) {
        this.check = false;
        this.value = startingValue;
    }

    public Counter(boolean check) {
        this.check = check;
    }

    public Counter() {
        this.value = 0;
        this.check = false;
    }

    public int value() {
        return this.value;
    }

    public void increase() {
        value++;
    }

    public void decrease() {
        if (this.check == true) {
            this.value--;
            if (value < 0) {
                value = 0;
            }
        } else if (this.check == false) {
            this.value--;
        }
    }

    public void increase(int IncreaseAmount) {
        if (IncreaseAmount >= 0) {
            this.value = value + IncreaseAmount;
        }
    }

    public void decrease(int DecreaseAmount) {
        if (DecreaseAmount >= 0) {
            this.value = value - DecreaseAmount;
        }
        if (check == true && value < 0) {
            value = 0;
        }
    }
}

现在,如果我要用这样的类执行一个主程序,例如:

Counter count = new Counter (2, true);
count.decrease();
count.decrease();
count.decrease();

我希望我的程序要做的是不要为负,因为布尔检查为真。然而它确实是-1。这是为什么?

4 个答案:

答案 0 :(得分:3)

您未能将全局变量check设置为false。您还使用了=而不是==:

使用:

public Counter(int startingValue, boolean check) {
    this.check = check;
    if (check == true) {
        value = startingValue;
        if (value < 0) {
            value = 0;
        }
    }
    else {
        value = startingValue;
    }    
}

答案 1 :(得分:0)

您需要使用==来比较相等性。使用单个=设置值。 更好的是,在检查布尔值时,只需使用布尔值。而不是

if (someBool == true)

喜欢

if (someBool)

同样,而不是

if (someBool == false)

喜欢

if (!someBool)

答案 2 :(得分:0)

你的if语句中的布尔测试需要在构造函数中使用==进行相等性比较。

在构造函数的第二个if语句中,您将check指定为false。

答案 3 :(得分:0)

使用布尔值执行布尔逻辑时,只需使用布尔值。

所以而不是 “if(this.check == true)”do“if(this.check)” 和 “if(this.check == false)”执行“if(!this.check)”

另外,对某些人你有“if(this.check = true)”,这对this.check赋予了真实。

你的主要问题是你错过了一个方法参数赋值给对象变量“this.check = check; //我添加了这个”

将您的版本与此进行比较:

public class Counter {

    private int value;
    private boolean check;

    public Counter(int startingValue, boolean check) {
        this.check = check; // I added this
        if (this.check) { //I changed this
            this.value = startingValue;
            if (value < 0) {
                value = 0;
            }
        } else { //and this
            this.value = startingValue;
        }

    }

    public Counter(int startingValue) {
        this.check = false;
        this.value = startingValue;
    }

    public Counter(boolean check) {
        this.check = check;
    }

    public Counter() {
        this.value = 0;
        this.check = false;
    }

    public int value() {  //good practice to use getVar and setVar, ie: getValue()
        return this.value;
    }

    public void increase() {
        value++;
    }

    public void decrease() {
        if (this.check) {  // you are not consistent with this.value VS value, which can be a confusing practise
            this.value--;
            if (value < 0) {
                value = 0;
            }
        } else {
            this.value--;
        }
    }

    public void increase(int increaseAmount) { //you had "IncreaseAmount", good practice to start vars with lower case
        if (increaseAmount >= 0) {
            this.value =  + increaseAmount;
        }
    }

    public void decrease(int decreaseAmount) {
        if (decreaseAmount >= 0) {
            this.value = value - decreaseAmount;
        }
        if (check && (value < 0)) {
            value = 0;
        }
    }

    public void print(){
        System.out.println("value:"+value+" check:"+check);
    }


    public static void main(String[] args) {
        Counter count = new Counter (2, true);
        count.decrease();
        count.print();

        count.decrease();
        count.print();

        count.decrease();
        count.print();

    }

}