只要有最大长度,如何返回“ 0”作为字符串?

时间:2019-11-29 08:36:23

标签: java oop

我的测试类有问题,如您在下面看到的输出,分钟返回一个我不希望出现的附加“ 0”,我相信问题出在我的NumberDisplay类中的getDisplayValue方法。在我尝试如果情况不正确的情况下,但是它给了我很多“ 0”,然后我的老师建议我对情况进行处理,所以我做到了,结果在我的测试班级比NumberDisplay要好。我在第2个测试类中尝试进行计时,这似乎很奇怪,如果您知道问题出在哪里,我将不胜感激


public class NumberDisplay {

    private int minLimit;
    private int maxLimit;
    private int value;
    private boolean wrapped;

    public NumberDisplay(int minLimit, int maxLimit) throws IllegalValueException {

        if (maxLimit > minLimit) {
            this.minLimit = minLimit;
            this.maxLimit = maxLimit - 1;
            this.value = minLimit;
            this.wrapped = true;
            // creates new number display with the value minimum limit
            // and the limits between maximum limit. if maximum limit is not greater than
            // minimum limit
            // it should throw exception

        } else {
            throw new IllegalValueException("felaktig");

        }

    }

    public int getValue() {
        return this.value;

        // return the current value on display with integer number
    }

    public void setValue(int newValue) throws IllegalValueException {
        if (newValue < minLimit || newValue >= maxLimit) {

            throw new IllegalValueException("Felaktig prova igen set time är felaktig");
            // set the current display value to newValue. if newValue is less greater than
            // minimum limit
            // or greater than value from display then throw exception
        } else {


                this.value = newValue;


        }

    }

    public String getDisplayValue() {
        String str = String.valueOf(this.maxLimit);
        String str1 = "0";


        if(str.length()==1) {
            return str1+this.value;
        }
        if(str.length()==2) {
            return str1+this.value;
        }
        if(str.length()==3) {
            return str1+str1+this.value;
        }
        if(str.length()==4) {
            return str1+str1+str1+this.value;
        }
        if(str.length()==5) {
            return str1+str1+str1+str1+this.value;
        }
        if(str.length()==6) {
            return str1+str1+str1+str1+str1+this.value;
        }else {
            for(int i =0;i<str.length();i++) {
                str1 = str1 +this.value;
            }
        }
        return ""+this.value;

    }


        // return the current value with 0 infront of the value

    public void increment() {

        if (this.value == maxLimit) {

            this.value = this.minLimit;
            this.wrapped = true;
        } else {

            this.value = +1;
        }

        // increases the display with one value and checks if maximum limit is reached
        // then
        // it should be converted to minimum limit

        // Consider to do the string delightful so all of values sets with as many as
        // numbers
        // the display should have. Values with low numbers should be zeros in front,
        // e.g. clock 9 09:00

    }

    public boolean diWrapAround() {
        if (this.value < maxLimit) {

            return this.wrapped = true;
        } else

            return false;

    }
    // it should return true if the display reached over maximum limit and started
    // from
    // minimum limit , otherwise it should return false,
    // use attribute wrapped to check boolean false or true.

}
public class Clock {

    private NumberDisplay hours;
    private NumberDisplay minutes;
    private String displayString;

    public Clock() throws IllegalValueException {

        this.hours = new NumberDisplay(0, 24);
        this.minutes = new NumberDisplay(0, 60);
        this.updateDisplay();

    }

    public Clock(int hour, int minute) throws IllegalValueException {
        this.hours = new NumberDisplay(0, 24);
        this.minutes = new NumberDisplay(0, 60);
        this.hours.setValue(hour);
        this.minutes.setValue(minute);

        this.updateDisplay();


    }

    public void timeTick() {

        this.minutes.increment();


    }

    public void setTime(int hour, int minute) throws IllegalValueException {
        this.hours.setValue(hour);
        this.minutes.setValue(minute);
        this.updateDisplay();

    }

    public String getTime() {
        return this.displayString;


    }

    private void updateDisplay() {

        this.displayString = this.hours.getDisplayValue()+":"+this.minutes.getDisplayValue();


    }

}
public class Test2 {

    public static void main(String[] args) {

        try {
            Clock klocka = new Clock(4343, 223);
            System.out.println(klocka.getTime());
        } catch (IllegalValueException a) {

            System.out.println(a.getMessage());
        }
        System.out.println("Expected: felmeddelande");
        System.out.println("");
        try {
            Clock klocka = new Clock(0, 24);
            System.out.println(klocka.getTime());
            System.out.println("Expected: 00:24");
            System.out.println("");
            klocka.timeTick();
            System.out.println(klocka.getTime());
            System.out.println("Expected: 00:01");
            System.out.println("");
            System.out.println(klocka.getTime());
            System.out.println("Expected to be 00:00");
            System.out.println("");
            try {
                klocka.setTime(123, 4554);
                System.out.println(klocka.getTime());
            } catch (IllegalValueException a) {

                System.out.println(a.getMessage());
            }
            System.out.println("Expected: Felmeddelande");
            System.out.println("");
            try {
                klocka.setTime(12, 23);
                System.out.println(klocka.getTime());
            } catch (IllegalValueException e) {
                System.out.println(e.getMessage());

            }
            System.out.println("Expected: 12:23");
            System.out.println("");

        } catch (IllegalValueException a) {

            System.out.println(a.getMessage());
        }

    }

}

输出

Felaktig prova igen set time är felaktig
Expected: felmeddelande

00:024
Expected: 00:24

00:024
Expected: 00:01

00:024
Expected to be 00:00

Felaktig prova igen set time är felaktig
Expected: Felmeddelande

012:023
Expected: 12:23

1 个答案:

答案 0 :(得分:1)

问题在于var strDate = new Date(); // By default Date empty constructor give you Date.now var shortYear = strDate.getFullYear(); // Add this line var twoDigitYear = shortYear.toString().substr(-2); 并没有考虑getDisplayValue本身的长度,而是完全根据value来做出决策。参见:

maxLimit

对于两位数显示,它将用零填充任何if(str.length()==2) { return str1+this.value; } 。因此,对于“ 0”,您将获得“ 00”;对于“ 12”,您将获得“ 012”。

要解决此问题,您需要考虑value的长度:

value

如果要避免循环,有更聪明的方法用零填充字符串。参见:Left padding a String with Zeros

P.S。在测试int limit = String.valueOf(maxLimit).length(); int length = String.valueOf(value).length(); StringBuilder displayValue = new StringBuilder(); //add a necessary number of zeros for(int i=0; i<limit-length; i++) { displayValue.append("0"); } displayValue.append(value); return displayValue.toString(); 之前,您可能应该单独测试NumberDisplay