繁体   English   中英

不兼容的类型,无法转换为 int

[英]Incompatible types, cannot be converted to int

谁能明白为什么我会收到错误“类型不兼容,NumberDisplay 无法转换为 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;
}
}

在这个方法中

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

正在尝试将NumberDisplay分配给seconds ,它在本地是参数变量并且是int 你应该把它改成

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

通常,在构造函数或 setter 中将参数命名为与成员变量相同的名称是可以的,但是您应该养成始终使用this.的习惯this. 在作业的左侧,以避免这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM