gpt4 book ai didi

javascript - 当militaryHour变量设置为0时,为什么系统打印0而不是我设置的小时(变量)12?

转载 作者:行者123 更新时间:2023-11-28 03:37:22 25 4
gpt4 key购买 nike

我正在尝试构建一个程序,将军事时间转换为上午或下午时间。

我预计 0 的输出是上午 12 点,但实际输出是上午 0 点。

const militaryHour = 0;

var hour = 0;
var amOrPm = "am";

if (militaryHour < 12) {
hour = militaryHour;
console.log(hour + " " + amOrPm);
} else if (militaryHour == 12) {
amOrPm = "pm";
hour = 12;
console.log(hour + " " + amOrPm);
} else if (militaryHour < 24) {
amOrPm = "pm";
hour = militaryHour - 12;
console.log(hour + " " + amOrPm);
} else if (militaryHour == 24){
hour = 12;
console.log(hour + " " + amOrPm);
} else {
hour = 12;
console.log(hour + " " + amOrPm);
}

最佳答案

您需要的所有代码

const militaryHour = 0,
hour = (militaryHour + 11) % 12 + 1,
amOrPm = militaryHour % 24 < 12 ? 'am' : 'pm';

不需要 if/else

关于javascript - 当militaryHour变量设置为0时,为什么系统打印0而不是我设置的小时(变量)12?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57583585/

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