gpt4 book ai didi

java - 将军事时间加在一起不起作用 - JAVA

转载 作者:行者123 更新时间:2023-12-02 04:46:14 26 4
gpt4 key购买 nike

我正在尝试用 Java 制作一个显示军事时间的时钟类,我的老师给了我们一个 ClockTester.java,它使用我们的时钟“引擎”。我已经让它正确显示,但我无法正确添加时间。
它全部以军事时间显示,因此添加 12:00:00 和 15:00:00 应该给我 03:00:00,而不是像它给我的 27:00:00...但我在做时遇到了麻烦那。

这是我的数学方法:

public void addHours(int h){
int sum = this.hours + h;
this.hours = (sum % 24);
if((this.hours = (sum % 24)) > 23) //Keeps the hours within real boundaries
this.hours -= 24;
}

public void addMinutes(int m){
int sum = this.mins + m;
this.mins = (sum % 60);
if((this.mins = (sum % 60)) > 59){ //Keeps the mins within real boundaries
this.mins -= 60;
this.addHours(sum % 60);
}
}

public void addSeconds(int s){
int sum = this.secs + s;
this.secs = (sum % 60);
if((this.secs = (sum % 60)) > 59){ //Keeps the secs within real boundaries
this.secs -= 60;
this.addMinutes(sum % 60);
}
}

如果您需要查看/了解其他内容来帮助我,请告诉我。谢谢。

最佳答案

this.hours += (sum % 24);

将结果(例如,小时为 23,h 为 5,因此 28%24 = 4,小时 = 23 + 4)添加到小时中。

你应该尝试:

this.hours = (sum % 24);

您可以对分钟和秒执行相同的操作。

关于java - 将军事时间加在一起不起作用 - JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29640390/

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