这是一项家庭作业。
我正在尝试创建一个带闹钟的java 12小时时钟。经过一周的不间断工作后,我终于让它运行了,但是,当我设置闹钟时,它会在设定的时间前2-3分钟熄灭。此外,当我打到贪睡(应该打盹2分钟)时,它会在下一分钟立即关闭。我总共有10个课程,但我添加的内容应该是相关的Time
和TimeManager
。我刚刚开始使用Java,我很难用它,很抱歉,如果这是一个愚蠢的问题。
Time
:
public class Time {
private int hour;
private int minute;
public Time(int initHour, int initMinute) {
if (1 <= hour && hour <= 12) {
hour = initHour;
}
else if (hour == 0) {
hour = 12;
}
if (0 <= minute && minute <= 59 ) {
minute = initMinute;
}
}
public void addOneMinute() {
minute++;
if (minute == 59) {
incrementHour();
minute = 0;
}
}
public void incrementHour() {
hour++;
if (hour > 12) {
hour = 1;
}
}
public void incrementMinute() {
minute++;
if (minute == 59) {
minute = 0;
}
}
public int getHour() {
if (hour >= 1 && hour <= 12) {
}
return hour;
}
public int getMinute() {
if (minute >=0 && minute <= 59) {
}
return minute;
}
public boolean equals(Time time) {
if (hour == hour && minute == minute) {
return true;
}
else {
return false;
}
}
}
TimeManager
:
public class TimeManager {
private static final int SNOOZE_DURATION_IN_MINUTES = 2;
private Time currentTime;
private Time alarmTime;
private Time snoozeTime;
private Display display;
private ModeManager modeMgr;
private Alarm alarm;
public TimeManager() {
currentTime = new Time(12,0);
alarmTime = new Time(12,0);
snoozeTime = new Time(12,0);
}
public void setDisplay(Display newDisplay) {
display = newDisplay;
showCurrentTime();
}
public void setModeManager(ModeManager newModeMgr) {
modeMgr = newModeMgr;
}
public void setAlarm(Alarm newAlarm) {
alarm = newAlarm;
}
public void incrementCurrentMinute() {
currentTime.addOneMinute();
Mode mode = modeMgr.getMode();
if (mode == mode.ALARM_ON || mode == mode.ALARM_OFF || mode == mode.SET_TIME) {
updateDisplay();
}
if (mode == mode.ALARM_ON) {
if (currentTime.equals(alarmTime) || currentTime.equals(snoozeTime)) {
soundAlarmIfNecessary();
}
}
}
public void incrementCurrentHour() {
currentTime.incrementHour();
Mode mode = modeMgr.getMode();
if (mode == mode.ALARM_ON || mode == mode.ALARM_OFF || mode == mode.SET_TIME) {
updateDisplay();
}
if (mode == mode.ALARM_ON) {
if (currentTime.equals(alarmTime) || currentTime.equals(snoozeTime)) {
soundAlarmIfNecessary();
}
}
}
public void incrementAlarmMinute() {
alarmTime.incrementMinute();
Mode mode = modeMgr.getMode();
if (mode == mode.SET_ALARM) {
updateDisplay();
}
if (mode == mode.ALARM_ON) {
if (currentTime.equals(alarmTime)) {
soundAlarmIfNecessary();
}
}
}
public void incrementAlarmHour() {
alarmTime.incrementHour();
Mode mode = modeMgr.getMode();
if (mode == mode.SET_ALARM) {
updateDisplay();
}
if (mode == mode.ALARM_ON) {
if (currentTime.equals(alarmTime)) {
soundAlarmIfNecessary();
}
}
}
public void snooze() {
while (alarm.isOn()) {
alarm.off();
int hour = currentTime.getHour();
int minute = currentTime.getMinute();
minute += SNOOZE_DURATION_IN_MINUTES;
if (minute > 59) {
minute -= 60;
++hour;
}
snoozeTime = new Time(hour, minute);
}
}
public void showCurrentTime() {
display.showHour(currentTime.getHour());
display.showMinute(currentTime.getMinute());
}
public void showAlarmTime() {
display.showHour(alarmTime.getHour());
display.showMinute(alarmTime.getMinute());
}
private void updateDisplay() {
Mode mode = modeMgr.getMode();
if (mode == mode.SET_TIME || mode == mode.ALARM_ON || mode == mode.ALARM_OFF) {
showCurrentTime();
}
else {
showAlarmTime();
}
}
private void soundAlarmIfNecessary() {
Mode mode = modeMgr.getMode();
if (mode == mode.ALARM_ON && currentTime.equals(alarmTime) || currentTime.equals(snoozeTime)) {
alarm.on();
}
}
}
答案 0 :(得分:2)
其他事情,你在Time.equals()
方法中有一个相当大的错误:
public boolean equals(Time time) {
if (hour == hour && minute == minute) {
return true;
}
else {
return false;
}
}
}
表达式hour == hour
正在将hour
实例变量与其自身进行比较,该变量始终为true
。这显然不是您想要的,而是您希望将其与给定hour
参数的time
字段进行比较,如下所示:
if (hour == time.getHour() && minute == time.getMinute() ) {