用java时钟打印的第60分钟

用java时钟打印的第60分钟,java,clock,Java,Clock,我不知道为什么会出现这种情况,但第60分钟会在结果中打印出来,而它应该在59分钟后的一小时内打印出来 这是我的课程和主要课程: public class Main { public static void main(String[] args) { BoundedCounter minutes = new BoundedCounter(59); BoundedCounter hours = new BoundedCounter(23);

我不知道为什么会出现这种情况,但第60分钟会在结果中打印出来,而它应该在59分钟后的一小时内打印出来

这是我的课程和主要课程:

public class Main {
    public static void main(String[] args) {
        BoundedCounter minutes = new BoundedCounter(59);
        BoundedCounter hours = new BoundedCounter(23);

        int i = 0;
        while (i < 121) {
            System.out.println(hours + ":" + minutes);   // the current time printed
            if (minutes.getValue() >= 59) {
                hours.getValue();
                i++;
            }
        }
    }
公共类主{
公共静态void main(字符串[]args){
BoundedCounter分钟数=新的BoundedCounter(59);
BoundedCounter小时数=新的BoundedCounter(23);
int i=0;
而(i<121){
System.out.println(小时+“:“+分钟);//打印的当前时间
如果(分钟数.getValue()>=59){
hours.getValue();
i++;
}
}
}
//边界计数器

    public class BoundedCounter {
        private int value;
        private int upperLimit;

        public BoundedCounter(int upperLimit) {
            this.upperLimit = upperLimit;
        }

        public void next() {
            if (this.value >= this.upperLimit) {
                this.value = 0;
            } else {
                this.value++;
            }
        }

        public String toString() {
            if (this.value < 10) {
                return "" + 0 + value;
            } else {
                return "" + value;
            }
        }

        public int getValue() {
            if (this.value <= this.upperLimit) {
                return this.value++;
            } else {
                return this.value = 0;
            }
        }
    }
}
公共类边界计数器{
私有int值;
私有整数上限;
公共边界计数器(整数上限){
这个。上限=上限;
}
下一个公共空间(){
如果(this.value>=this.upperLimit){
该值=0;
}否则{
这个.value++;
}
}
公共字符串toString(){
如果(该值小于10){
返回“”+0+值;
}否则{
返回“”+值;
}
}
public int getValue(){

如果(this.value您应该更改行:

if (this.value <= this.upperLimit) {
    return this.value++;


问题是,当代码中的分钟数值为59时,仍然没有显示实际增加小时数或分钟数的任何部分,但在getValue调用中似乎存在问题

public int getValue() {
    if (this.value <= this.upperLimit) {
        return this.value++;
    } else {
        return this.value = 0;
    }
}
public int getValue(){

如果(this.value在创建BoundedCounter的方法中存在轻微问题 当方法名称表示只想读取计数器值时,不应更改计数器值。其次,所有重置条件检查只应在BoundedCounter类内进行。如果要重置计数器,只需公开另一个方法即可

以下是工作解决方案供您参考

import java.util.*;

public class Test{

    public static void main(String[] args){
        BoundedCounter minutes = new BoundedCounter(59);
        BoundedCounter hours = new BoundedCounter(23);
        int i = 0;
        while (i < 121) {
            if (minutes.hasNext()) {
                minutes.next();
            } else {
                hours.next();
                minutes.resetTo(0);
            }
            System.out.println(hours + ":" + minutes);
            i++;
        }
    }
}

class BoundedCounter {
    private int value;
    private int upperLimit;

    public BoundedCounter(int upperLimit) {
        this.upperLimit = upperLimit;
    }

    public void next() {
        ++this.value;
    }

    public boolean hasNext() {
        return this.value < this.upperLimit;
    }

    public void resetTo(int resetValue) {
        this.value = resetValue;
    }

    public int getValue() {
        if (this.hasNext()) {
            return this.value;
        }
        return 0;
    }

    public String toString() {
        if (this.value < 10) {
            return "" + 0 + value;
        } else {
            return "" + value;
        }
    }

}
import java.util.*;
公开课考试{
公共静态void main(字符串[]args){
BoundedCounter分钟数=新的BoundedCounter(59);
BoundedCounter小时数=新的BoundedCounter(23);
int i=0;
而(i<121){
if(minutes.hasNext()){
分钟。下一步();
}否则{
小时。下一个();
分钟。重置为(0);
}
系统输出打印项次(小时+“:”+分钟);
i++;
}
}
}
类边界计数器{
私有int值;
私有整数上限;
公共边界计数器(整数上限){
这个。上限=上限;
}
下一个公共空间(){
++这就是价值;
}
公共布尔hasNext(){
返回this.value
首先,您的方法正在做它们不应该做的事情。例如,
getValue()
应该只返回值,而不是递增值。下面是如何做到这一点:

Main.java

一些产出:


00:00:01 00:02 00:03 00:04 00:05 00:06 00:07 00:08 00:09 00:10 00:11 00:12 00:13 00:14 00:15 00:16 00:17 00:18 00:19 00:20 00:21 00:22 00:23 00:24 00:25 00:26 00:27 00:28 00:29 00:30 00:31 00:32 00:33 00:34 00:35 00:36 00:37 00:38 00:39 00:40 00:41 00:42 00:43 00:44 00:45 00:46 00:47 00:48 00:49 00:50 00:51 00:52 00:53 00:54 00:55 00:56 00:57 00:5800:59 01:0001:01:02:03 01:04:05 01:06 01:07 01:08 01:09 01:10 01:11 01:12 01:13 01:14 01:15 01:16 01:17 01:18 01:19 01:20 01:21 01:22 01:23 01:24 01:25 01:26 01:27 01:28 01:29 01:30 01:31 01:32 01:33 01:34 01:35 01:36 01:37 01:38 01:39 01:40 01:41 01:42 01:43 01:44 01:45 01:46 01:47 01:48 01:49 01:50 01:51 01:52 01:53 01:54 01:55 01:56 01:57 01:5801:59 02:00 02:01


不,这会导致一个新的错误:23:53 23:54 23:55 23:56 23:57 23:58 00:59 00:0000:00:00:01 00:02 00:03 00:04 00:05 00:06 00:07 00:08 00:09 00:10 00:11 00:12 00:13 00:14 00:15 00:16 00:17 00:18 00:19 00:20 00:21 00:22 00:23 00:24 00:25 00:26 00:27 00:32 00:33 00:34 00:35 00:38:40:4300:44 00:45 00:46 00:47 00:48 00:49 00:50 00:51 00:52 00:53 00:54 00:55 00:56 00:57 00:58 00:59 00:00:00:01 00:02 00:03 00:04 00:05有点像这样:(它在无限空间中重复loop@Nebular现在可以了吗?就名称而言,您的方法正在做不应该做的事情。请参阅我对您的问题的解决方案。
if (minutes.getValue() == 0) {
public int getValue() {
    if (this.value <= this.upperLimit) {
        return this.value++;
    } else {
        return this.value = 0;
    }
}
import java.util.*;

public class Test{

    public static void main(String[] args){
        BoundedCounter minutes = new BoundedCounter(59);
        BoundedCounter hours = new BoundedCounter(23);
        int i = 0;
        while (i < 121) {
            if (minutes.hasNext()) {
                minutes.next();
            } else {
                hours.next();
                minutes.resetTo(0);
            }
            System.out.println(hours + ":" + minutes);
            i++;
        }
    }
}

class BoundedCounter {
    private int value;
    private int upperLimit;

    public BoundedCounter(int upperLimit) {
        this.upperLimit = upperLimit;
    }

    public void next() {
        ++this.value;
    }

    public boolean hasNext() {
        return this.value < this.upperLimit;
    }

    public void resetTo(int resetValue) {
        this.value = resetValue;
    }

    public int getValue() {
        if (this.hasNext()) {
            return this.value;
        }
        return 0;
    }

    public String toString() {
        if (this.value < 10) {
            return "" + 0 + value;
        } else {
            return "" + value;
        }
    }

}
public class Main {
    public static void main(String[] args) {
        BoundedCounter minutes = new BoundedCounter(59);
        BoundedCounter hours = new BoundedCounter(23);

        int i = 0;
        while (i < 121) {
            System.out.println(hours + ":" + minutes);

            minutes.next();                 // counting minutes
            if (minutes.getValue() == 0) {  // when minutes==0, count hours
                hours.next();               // counting hours
                i++;
            }
        }
    }
}
class BoundedCounter {
    private int value;
    private int upperLimit;

    public BoundedCounter(int upperLimit) {
        this.upperLimit = upperLimit;
    }

    public void next() {
        // when reach the upperLimit, the next value should be 0
        // so >= is not needed, just == will do
        this.value = this.value == this.upperLimit ? 0 : this.value+1;
    }

    public String toString() {
        // using smarter approach to pad with zeros :)
        return String.format("%02d", value);
    }

    public int getValue() {
        // this method should only return the value, not change it in any way
        return this.value;
    }
}