2017-03-08 84 views
-2

有人能看到爲什麼我收到錯誤「不兼容的類型,NumberDisplay不能轉換成int」類型不兼容,不能轉換成int

原來的項目沒有包括秒,所以我想補充它,但是當我嘗試時,我得到了錯誤。

我試過改變數值,沒有幫助。

public class ClockDisplay 
{ 
private NumberDisplay hours; 
private NumberDisplay minutes; 
private NumberDisplay seconds; 
private String displayString; // simulates the actual display 

/** 
* Constructor for ClockDisplay objects. This constructor 
* creates a new clock set at 00:00. 
*/ 
public ClockDisplay() 
{ 
    hours = new NumberDisplay(24); 
    minutes = new NumberDisplay(60); 
    seconds = new NumberDisplay(60); 
    updateDisplay(); 
} 

/** 
* Constructor for ClockDisplay objects. This constructor 
* creates a new clock set at the time specified by the 
* parameters. 
*/ 
public ClockDisplay(int hour, int minute, int seconds) 
{ 
    hours = new NumberDisplay(24); 
    minutes = new NumberDisplay(60); 
    seconds = new NumberDisplay(60); 
    setTime(hour, minute); 
} 

/** 
* This method should get called once every minute - it makes 
* the clock display go one second forward. 
*/ 
public void timeTick() 
{ 
    seconds.increment(); 
    if(seconds.getValue() == 0) { // it just rolled over! 
     minutes.increment(); 
    } 
    updateDisplay(); 
} 

/** 
* This method should get called once every minute - it makes 
* the clock display go one minute forward. 
*/ 
public void timeTickTwo() 
{ 
    minutes.increment(); 
    if(minutes.getValue() == 0) { // it just rolled over! 
     hours.increment(); 
    } 
    updateDisplay(); 
} 

/** 
* Set the time of the display to the specified hour and 
* minute. 
*/ 
public void setTime(int hour, int minute, int seconds) 
{ 
    hours.setValue(hour); 
    minutes.setValue(minute); 
    seconds.setValue(second); 
    updateDisplay(); 
} 

/** 
* Return the current time of this display in the format HH:MM. 
*/ 
public String getTime() 
{ 
    return displayString; 
} 

/** 
* Update the internal string that represents the display. 
*/ 
private void updateDisplay() 
{ 
    displayString = hours.getDisplayValue() + ":" + 
        minutes.getDisplayValue() + ":" + 
        seconds.getDisplayValue(); 
} 



public class NumberDisplay 
{ 
private int limit; 
private int value; 

/** 
* Constructor for objects of class NumberDisplay. 
* Set the limit at which the display rolls over. 
*/ 
public NumberDisplay(int rollOverLimit) 
{ 
    limit = rollOverLimit; 
    value = 0; 
} 

/** 
* Return the current value. 
*/ 
public int getValue() 
{ 
    return value; 
} 

/** 
* Return the display value (that is, the current value as a two-digit 
* String. If the value is less than ten, it will be padded with a leading 
* zero). 
*/ 
public String getDisplayValue() 
{ 
    if(value < 10) { 
     return "0" + value; 
    } 
    else { 
     return "" + value; 
    } 
} 

/** 
* Set the value of the display to the new specified value. If the new 
* value is less than zero or over the limit, do nothing. 
*/ 
public void setValue(int replacementValue) 
{ 
    if((replacementValue >= 0) && (replacementValue < limit)) { 
     value = replacementValue; 
    } 
} 

/** 
* Increment the display value by one, rolling over to zero if the 
* limit is reached. 
*/ 
public void increment() 
{ 
    value = (value + 1) % limit; 
} 
} 
+1

您需要編輯您的文章並添加'NumberDisplay'代碼。此外,請告訴我們哪條線可以給您提供錯誤。 –

+1

這是否編譯?你似乎用錯誤數量的參數調用'setTime'。 – Berger

回答

4

在該方法中

public ClockDisplay(int hour, int minute, int seconds) 
               ^^^^^^^ 
{ 
    hours = new NumberDisplay(24); 
    minutes = new NumberDisplay(60); 
    seconds = new NumberDisplay(60); 
    ^^^^^^^ 
    setTime(hour, minute); 
} 

seconds = new NumberDisplay(60); 

試圖分配一個NumberDisplayseconds,這是局部參數變量,並且是int。你應該將其更改爲

this.hours = new NumberDisplay(24); 
    this.minutes = new NumberDisplay(60); 
    this.seconds = new NumberDisplay(60); 

一般是在構造函數或setter OK命名的參數一樣的成員變量,但你應該進入始終在分配的左手使用this.的習慣以避免這個問題。

+1

它是最好的變量陰影 – Zeromus

+0

並且是構造函數/設置函數中的一種公認的技術,可以防止不必要地創建額外的變量名稱,從而增加認知負載。 –

相關問題