我不知道爲什麼發生這種情況,但在第60分鐘被印在結果時,應該分鐘後變成小時5960分鐘印刷在Java時鐘
這是我的階級和主程序:
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是不可預測的輸出
你的方法在做什麼,他們不應該做的事情,至於他們的名字擔心。查看我對您問題的解決方案。 –