java - int 不能转换为布尔值?
问题描述
这是我的计算机科学课作业的一部分。我是一名新程序员,我被困在检查警报方法上。它应该检查 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
工作,“小时”必须是一个int
或不能装箱成一个int
like 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
具有.getValue()
返回int
. _
推荐阅读
- c++ - 查找字符串是否包含字符串向量的任何一个元素的最佳方法
- php - TypeRocket:通过控制器调用函数时,php脚本停止运行
- ruby - 向另一个 Slack 机器人发送消息
- android - 在 Android 类 BaseAdapter 的 getItemId 方法中返回字符串值
- c# - 在针对开发人员沙箱而不是嵌入式签名的身份验证之后?
- python - 根据长度从字符串中删除最后一个数字
- jquery - Smartystreets 不会自动填充邮政编码
- node.js - Mongo $or 查询同时使用 $exists 和 $eq
- java - 如何改变结果的输出?
- reactjs - Typescript 和 React 具有复杂的 prop 类型接口