2017-05-31 61 views
1

我正在使用Java編寫一個程序,用於能夠「打勾」但能解決問題的時鐘。我認爲它與getter和setter或toString()方法有關。能夠「打勾」的時鐘

計數器類

package clock; 

public class Counter 
{ 
private int _count; 
private String _name; 

public String getName(){return _name;} 
public void setName(String _name){this._name = _name;} 

public int getCount(){return _count;} 
public void setCount(int _count){this._count = _count;} 

public Counter(String name) 
{ 
    _name = name; 
    _count = 0; 
} 

public void Increment() 
{ 
    _count++; 
} 

public void Reset() 
{ 
    _count = 0; 
} 
} 

時鐘類

package clock; 

import java.util.Calendar; 


public class Clock { 
private Counter _secCounter; 
private Counter _minCounter; 
private Counter _hrCounter; 

public Clock() 
{ 
    this._secCounter = new Counter("Seconds"); 
    this._minCounter = new Counter("Minutes"); 
    this._hrCounter = new Counter("Hour"); 

    Calendar currentTime = Calendar.getInstance(); 
    _secCounter.setCount(currentTime.get(Calendar.SECOND)); 
    _minCounter.setCount(currentTime.get(Calendar.MINUTE)); 
    _hrCounter.setCount(currentTime.get(Calendar.HOUR)); 
} 

public Counter SecCounter(){return _secCounter;} 
public Counter MinCounter(){return _minCounter;} 
public Counter HrCounter(){return _hrCounter;} 

public String Tick() 
{ 
    _secCounter.Increment(); 
    if (_secCounter.getCount() == 60) 
    { 
     _secCounter.Reset(); 
     _minCounter.Increment(); 
    } 

    if (_minCounter.getCount() == 60) 
    { 
     _minCounter.Reset(); 
     _hrCounter.Increment(); 
    } 

    if (_hrCounter.getCount() == 24) 
    { 
     _hrCounter.Reset(); 
    } 

    return _hrCounter.toString() + ":" + _minCounter.toString() + ":" + _secCounter.toString(); 
} 

@Override 
public String toString() 
{ 
    return _hrCounter.toString() + ":" + _minCounter.toString() + ":" + _secCounter.toString(); 
} 
} 

主類

package clock; 

public class Main { 

public static void main(String[] args) 
{ 
    Clock myClock = new Clock(); 
    for (int i = 0; i < 10; i++) 
    { 

     String currentTime = myClock.Tick(); 
     System.out.println(currentTime); 
     i = 0; 
    } 


} 
} 

它輸出

[email protected]:[email protected]:[email protected] 

剛剛接觸Java並且正在翻譯來自C#的代碼。謝謝您的幫助!

+3

請在時鐘和tick類中覆蓋toString方法[檢查此(https://www.javatpoint.com/understanding-toString() - 方法) –

+0

您必須重寫'Counter'類中的'toString'方法 –

回答

1

據我所知,你需要調用_ **計數器。 getCount(),而不是toString()。你從不重寫toString方法,所以爲了得到你的時鐘值,你需要使用你寫的方法來獲得計數器的值。如果你想要報告它的名字,你需要覆蓋Counter中的toString()方法來返回類似於return getName() + ": " + getCount();

+0

如果不清楚,我的意思是_ ** Counter是計數器的名稱。所有的櫃檯都遵循這個名稱模式,所以我隨它而行。 –

0

我推薦使用java.util.Timer來每隔1秒鐘執行一次Clock.Tick()方法。

public static void main(String[] args) { 
     Clock myClock = new Clock(); 
     Timer timer = new Timer(); 
     timer.schedule(new TimerTask() { 

      @Override 
      public void run() { 
       String currentTime = myClock.Tick(); 
       System.out.println(currentTime); 
      } 
     }, 0, 1000); 
    } 

您首先創建新的Timer實例。調用其schedule方法使TimerTask每1000毫秒運行一次,初始延遲爲0.