我不确定为什么会发生这种情况,但第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++;
}
}
}
// BoundedCounter
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;
}
}
}
}
一些结果:
01:56
01:57
01:58
01:59
02:60
02:00
02:01
02:02
02:03
02:04
02:05
02:06
02:07
02:08
02:09
02:10
02:11
02:12
02:13
02:14
02:15
02:16
02:17
02:18
02:19
02:20
02:21
02:22
02:23
02:24
02:25
02:26
02:27
02:28
02:29
02:30
02:31
02:32
02:33
02:34
02:35
02:36
02:37
02:38
02:39
02:40
02:41
02:42
02:43
02:44
02:45
02:46
02:47
02:48
02:49
02:50
02:51
02:52
02:53
02:54
02:55
02:56
02:57
02:58
02:59
03:60
03:00
03:01
03:02
03:03
03:04
03:05
即2:60和3:60是意外输出