这是我的计算机科学课家庭作业的一部分。我是一名新程序员,并且无法使用检查警报方法。应该检查aHours是否等于小时,aMinutes是否等于分钟,而aSeconds是否等于秒。它一直给我一个错误消息,提示“二进制运算符'=='的错误操作数类型,第一种:int第二种:NumberDisplay”
代码如下:
/**
* The ClockDisplay class implements a digital clock display for a
* European-style 24 hour clock. The clock shows hours and minutes. The
* range of the clock is 00:00 (midnight) to 23:59 (one minute before
* midnight).
*
* The clock display receives "ticks" (via the timeTick method) every minute
* and reacts by incrementing the display. This is done in the usual clock
* fashion: the hour increments when the minutes roll over to zero.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private NumberDisplay seconds;
private String displayString; // simulates the actual display
private int aHours;
private int aMinutes;
private int aSeconds;
private boolean isSet;
/**
* 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 second)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
seconds = new NumberDisplay(60);
setTime(hour, minute, second);
}
/**
* This method should get called once every minute - it makes
* the clock display go one minute forward.
*/
public void timeTick()
{
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 second)
{
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();
}
/**
* Set alarm for clock
*/
private void setAlarm(int phour, int pminute, int psecond)
{
aHours = phour;
aMinutes = pminute;
aSeconds = psecond;
isSet = true;
System.out.println("Alarm is set");
}
/**
* Cancel alarm for clock
*/
private void cancelAlarm()
{
isSet = false;
System.out.println("Alarm is off");
}
/**
* Check alarm for clock
*/
private void checkAlarm()
{
if(aHours == hours && aMinutes == minutes && aSeconds == seconds)
{
System.out.println("Alarm is off");
return true;
}
else
{
return false;
}
}
}
这是错误所在:
if(aHours == hours && aMinutes == minutes && aSeconds == seconds)
{
System.out.println("Alarm is off");
return true;
}
else
{
return false;
}
答案 0 :(得分:1)
在您的代码中,您拥有:
private NumberDisplay hours; // a variable called "hours" that is of type NumberDisplay
private NumberDisplay minutes; // a variable called "minutes" that is of type NumberDisplay
private NumberDisplay seconds; // a variable called "seconds" that is of type NumberDisplay
private int aHours; // a variable called "aHours" that is of type int
private int aMinutes; // a variable called "aMinutes" that is of type int
private int aSeconds; // a variable called "aSeconds" that is of type int
要使aHours == hours && aMinutes == minutes && aSeconds == seconds
工作,“小时数”必须是int
或不可装箱到int
,例如Integer
。分钟和秒都一样。
您想要获得的是由以下每种存储的int值:
如果(aHours == hours.getValue()&& aMinutes == minutes.getValue()&& aSeconds == seconds.getValue()){
如您在
中所见public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
方法minutes
有一个.getValue()
方法可返回int
。
答案 1 :(得分:0)
您发布的代码几乎没有问题。
但是,要回答您的问题,您会收到错误消息,因为您试图将int
与NumberDisplay
进行比较以查看它们是否相等。
在Java中,您不能这样做。相反,您需要执行以下操作之一:
a)将int
转换为NumberDisplay
,然后使用NumberDisplay.equals()比较它们(但您可能必须自己编写该方法-尝试研究“在Java中重写等于”)
b)将NumberDisplay
转换为int
,然后使用当前使用的==
运算符进行比较。转换为int
的方式取决于NumberDisplay
代码的作用。
c)写一个知道如何比较NumberDisplay
和int
的方法,并使用它。确切的工作方式取决于NumberDisplay
的代码,但是通常您会在NumberDisplay
中创建一个带有如下签名的方法:
public boolean isTheSame(int compare)
正如注释中所指出的那样,您还需要更改代码,以使任何返回true
或false
的方法都具有boolean
而不是{ {1}}
答案 2 :(得分:0)
需要更改两件事:函数返回类型应为boolean
。
您需要获取NumberDisplay元素的值。由于我没有NumberDisplay的代码,因此我假设您可以使用getValue
。这给出了以下代码:
/**
* Check alarm for clock
*/
private boolean checkAlarm()
{
if(aHours == hours.getValue() && aMinutes == minutes.getValue() && aSeconds == seconds.getValue())
{
System.out.println("Alarm is off");
return true;
}
else
{
return false;
}
}