简体   繁体   English

为什么我的 count 方法不应该返回 false ?

[英]Why does not my count method return false when it is supposed to?

I have coded a class where you have a start and an end as inputs, and where a counter counts either up or down depending on whether the start value is smaller or bigger than the end value.我编写了一个 class 代码,其中您有一个开始和结束作为输入,并且计数器根据起始值是小于还是大于结束值来向上或向下计数。 When the counter has reached the end value, it should return false, if not it should return true.当计数器达到结束值时,它应该返回 false,如果没有,它应该返回 true。

When I run this in a junit test my professor has made, it says "After reaching the end value, the count() method should return false".当我在教授所做的 junit 测试中运行它时,它说“达到最终值后,count() 方法应该返回 false”。 I don't understand what is wrong with code.我不明白代码有什么问题。

public class UpOrDownCounter {

    private int end;
    private int counter;
    private int start;
    private int increment;

    UpOrDownCounter(int start, int end){
        this.start = start;
        this.end = end;
        this.counter = start;

        isValid();

        if(start<end) {
            this.increment = 1;
        }
        else {
            this.increment = (-1);
        }
    }

    public void isValid() throws IllegalArgumentException{
        if(this.start == this.end) {
            throw new IllegalArgumentException("the start value can't be the same as the end value");
        }
    }

    public int getCounter() {
        return counter;
    }

    public boolean count() {
        counter += increment;
        if(this.counter == end) {
            this.counter == 0;
            return false;
        }
        else {
            return true;
        }
    }

    public String toString() {
        return "[counter = " + counter + "]";
    }

    public static void main(String [] args){
        UpOrDownCounter c = new UpOrDownCounter(3, 5);
        System.out.println(c.toString());
}

What happens to this.increment if start == end ?如果start == endthis.increment会发生什么? We need to call isValid(), so that the counter doesn't blow past either end of the range.我们需要调用 isValid(),以便计数器不会超出范围的任何一端。 Change your constructor to:将您的构造函数更改为:

UpOrDownCounter(int start, int end) throws IllegalArgumentException {
    this.start = start;
    this.end = end;
    this.counter = start;

    isValid();

    if(start<end) {
        this.increment = 1;
    } else {
        this.increment = (-1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我更改数组的元素时,为什么我的方法不返回false? - Why does my method not return false when I change the array's element? 当两个对象相同时,为什么equals()方法返回false? - Why does the equals() method return false when the two objects are identical? 为什么这个方法总是返回 false? - Why does this method always return false? 当返回值不同时,为什么我的排序方法返回 0? - Why does my sort method return 0 when return value is different? 为什么我的方法在我理解应该返回 true 时返回 false? - Why is my method returning false when it should return true for my understanding? 为什么 java.util.Scanner next().charAt(0) 返回“{”,而我的 nextCharIs(scanner,“{”) 方法返回 false? - Why does java.util.Scanner next().charAt(0) return “{”, yet my nextCharIs(scanner,“{”) method returns false? 为什么即使我的条件为假,我的方法也会不断重复? - Why does my Method keep repeating even when my condition is false? 为什么这个String.equals()方法总是返回false? - Why does this String.equals() method always return false? 为什么在我的蛇游戏中对象的比较返回 false - Why does comparison of objects return false in my snake game 为什么我的 Validare Zip Code(boolean) 程序返回 false? - Why does my Validare Zip Code(boolean) program return false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM