向时钟添加时间(不工作)-JAVA

向时钟添加时间(不工作)-JAVA,java,class,methods,time,clock,Java,Class,Methods,Time,Clock,我正试图用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; //Keeps the h

我正试图用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;   //Keeps the hours value within real boundaries
    this.hours += (sum % 24);
}

public void addMinutes(int m){
    int sum = this.mins + m;
    this.addHours(m / 60);
    this.mins += (sum % 60);
}

public void addSeconds(int s){
    int sum = this.secs + s;
    this.addMinutes(s / 60);
    this.secs += (sum % 60);
}

public void addTime(int h, int m, int s){
    addHours(h);
    addMinutes(m);
    addSeconds(s);
}

如果你还需要看/知道什么来帮助我,请告诉我。谢谢。

如果你在add hours方法中以15开始,那么首先要取24的模数,也就是12。然后把它加到你原来的数额上,得到27。你需要做的是添加一个检查,看看你是否通过了,24或60,而不是让它添加,让它重置

public void addHours(int h){
        int sum = this.hours + h;
        this.hours += (sum % 24);
    }

你应该做的是包括一个while循环,然后循环下来。当时间大于24:00:00(或其等效单位为秒:84600)时,应减去24小时。在addTime(…){…}中添加:

while (hours*3600 + mins*60 + secs > 84600) {
    hours -= 24;
}
您也可以使用if()语句,因为时间永远不会超过48小时。但为了安全起见,利用这段时间。 If语句格式:

if (hours*3600 + mins*60 + secs > 84600) {
    hours -= 24;
}

恐怕我还是不知道该怎么做。。。现在我有:(看上面的帖子编辑)