gpt4 book ai didi

Javabat 帮助 : alarmClock

转载 作者:行者123 更新时间:2023-12-04 06:04:43 29 4
gpt4 key购买 nike

我正在解决 JavaBat 问题并且对我的逻辑感到困惑。

这是任务:

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring. Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".

alarmClock(1, false) → "7:00" alarmClock(5, false) → "7:00" alarmClock(0, false) → "10:00"



这是我的代码:
            public String alarmClock(int day, boolean vacation) {
if ( (day >=1 && day <=5) && (!vacation)) {
return "7:00";
} else if ( (day >=1 && day <=5) && (vacation)) {
return "10:00";
} else {
return "off";
}
}

为什么这两个测试失败?

alarmClock(0, false) → "10:00""off"X
alarmClock(6, false) → "10:00""off"X

当然,这条线涵盖了它?

if (day >=1 && day <=5) && (!vacation))

最佳答案

这个怎么样?

      public String alarmClock(int day, boolean vacation) {
if (day >=1 && day <=5) {
return vacation ? "10:00" : "7:00";
} else {
return vacation ? "off" : "10:00";
}
}

请注意,这取决于您的编码约定是否允许使用 Turnary 运算符。但在这种情况下,我认为逻辑更容易阅读。

关于Javabat 帮助 : alarmClock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8491894/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com