2016-02-26 52 views
4

我不知道爲什麼發生這種情況,但在第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是不可預測的輸出

+0

你的方法在做什麼,他們不應該做的事情,至於他們的名字擔心。查看我對您問題的解決方案。 –

回答

2

首先的工作溶液中,你的方法在做什麼,他們不應該做的事。例如,getValue()應只返回該值,而不是遞增值。下面是如何可以做到這一點:

Main.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); 

      minutes.next();     // counting minutes 
      if (minutes.getValue() == 0) { // when minutes==0, count hours 
       hours.next();    // counting hours 
       i++; 
      } 
     } 
    } 
} 

BoundedCounter.java

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; 
    } 
} 

一些輸出:

00: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:58 00:59 01:00 01:01 01:02 01:03 01:04 01: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:58 01:59 02:00 02:01

1

您應該更改行:

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

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

if (minutes.getValue() >= 59) { 

if (minutes.getValue() == 0) { 

問題是,當代碼中的分鐘值爲59時,它仍然是< = 59,因此它會進入if塊。然後你回到59的用戶,現在分鐘數60

所以,在接下來的迭代中,您先打印60 然後調用getValue()方法,它把你的分鐘到0

我想你在小時變成24時有同樣的問題...

除此之外,考慮遵循一個更「預期」的方法功能,根據他們的名字,這使得它清楚他們做什麼和它即使對您進行調試也變得更容易。例如,請參見Shadowfax's answer

+1

不,這個人讓一個新的錯誤:23:53 23:54 23:55 23:56 23:57 23:58 00:59 00:00 – Nebular

+0

00:00 00:01 00:02 00:03 00:04 00:05 0時06分 0點07 00:08 00:09 00:10 零點11 ○點12 00:13 00 :14 00:15 00:16 00:17 00:18 0點19 00:20 0時21分 00:22 0點23 0點24 0點25 0時26 零時27 0時28分00 :29 00:30 0點31 00:32 0點33分 00:34 ○時35分 0時36分 0點37分 00:38 0點39 00:40 00:41 00:42 0時43 0點44 00:45 00:46 0點47 零點48 0時49 零點50 0時51分 0點52 0時53分 0點54 00 :55 00:56 00:57 00:58 00:59 00:00 00:01 00:02 00:03 00:04 00:05排序的確實是:( – Nebular

+0

它在無限循環中類似的重複 – Nebular

0

你沒有顯示實際增加你的小時或分鐘的任何部分,但它似乎是你的getValue調用中的一個問題。

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

this.value++;this.value++;部分將值遞增到60,並且不滾動它。

0

在創建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; 
     } 
    } 

}