当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Counter: decrease method disregards boolean

问题描述:

I am trying to create a counter that holds a number that can be increased and decreased. The program also has a boolean check: when true, the counter cannot go negative. The program seems to run fine, but I cannot get the decrease methods (both decrease by one and decrease by input) to get the boolean right. It does not check the boolean value or something? I am new to Java and need help understanding what is wrong. The class is as follows:

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;

}

}

}

Now, if I was to execute a main program with this class like this for example:

Counter count = new Counter (2, true);

count.decrease();

count.decrease();

count.decrease();

What I want my program to do is to not go negative since the boolean check is true. Yet it does go to -1. Why is this?

网友答案:

You fail to set the global variable check to false. You also used = instead of ==:

use:

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

You need to use == to compare equality. The use of a single = sets the value. Better yet, when checking the value of a boolean, just use the boolean. So instead of

if (someBool == true)

prefer

if (someBool)

Similarly, instead of

if (someBool == false)

prefer

if (!someBool)
网友答案:

Your boolean tests in your if statements need to use == for equality comparison in your constructor.

In your constructor's second if statement, you are assigning check to false.

网友答案:

When performing boolean logic with a boolean, just use the boolean.

So instead of "if (this.check == true)" do "if (this.check)" and "if (this.check == false)" do "if (!this.check)"

Also, you had "if (this.check = true)" for some, which assigns true to this.check.

You main issue is that you missed an assignment of an method parameter to the object variable "this.check = check; // I added this"

Compare your version with this:

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

    }

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: