[英]int can't be converted to boolean?
这是我的计算机科学 class 作业的一部分。 我是一名新程序员,我被困在检查警报方法上。 它应该检查 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;
}
在您的代码中,您有:
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
的工作,“hours”必须是一个int
或不能装箱成一个int
像Integer
。 分钟和秒也一样。
您想要的是获取其中每个存储的 int 值:
if (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
的方法有一个返回int
的.getValue()
方法。
您发布的代码几乎没有问题。
但是,要回答您的问题,您会收到错误消息,因为您正在尝试将int
与NumberDisplay
进行比较以查看它们是否相等。
在 java 中,您不能这样做。 相反,您需要执行以下操作之一:
a)将int
转换为NumberDisplay
,然后使用 NumberDisplay.equals() 比较它们(但您可能必须自己编写该方法 - 尝试研究“在 java 中覆盖 Equals”)
b) 将NumberDisplay
转换为int
,然后使用您当前使用的==
运算符比较它们。 如何转换为int
取决于NumberDisplay
代码的作用。
c) 编写一个知道如何将NumberDisplay
与int
进行比较的方法,并使用它。 这究竟是如何工作的,取决于NumberDisplay
的代码是什么样的,但通常你会在NumberDisplay
中创建一个带有如下签名的方法:
public boolean isTheSame(int compare)
正如评论中指出的那样,您还需要更改代码,以便任何返回true
或false
的方法都具有boolean
的“返回类型”而不是void
需要更改两件事: function 返回类型应为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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.