首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java中的军用时差

Java中的军用时差
EN

Stack Overflow用户
提问于 2018-10-12 02:32:09
回答 3查看 1.5K关注 0票数 8

下面是我的TimeInterval类:

代码语言:javascript
复制
public class TimeInterval {

   private int fTime;
   private int sTime;

   public TimeInterval(int fTime, int sTime) {
      if(fTime < 0 || fTime > 2400 || sTime < 0 || sTime > 2400) {
          System.out.println("Illegal times, must be < 2400 and > 0)");
          System.exit(0);
      } else {
          this.fTime = fTime;
          this.sTime = sTime;
      }
   }

   public int getHours() {
      return Math.abs((fTime - sTime) / 100);
   }

   public int getMinutes() {
       return Math.abs((fTime - sTime) % 100);
   }

   public double getDecimalTime() {
      return getHours() + ((double) getMinutes() / 60);
   }
}

和我的测试人员类:

代码语言:javascript
复制
import java.util.*;

public class TestTimeInterval {

   public static void main(String[] args) {

      Scanner s = new Scanner(System.in);
      System.out.print("Please enter the first time: ");
      int fTime = s.nextInt();
      System.out.print("Please enter the second time: ");
      int sTime = s.nextInt();

      TimeInterval t = new TimeInterval(fTime, sTime);

      System.out.printf("%s: %2d hours %2d minutes \n", "Elapsed time in hrs/min ", t.getHours(), t.getMinutes());
      System.out.printf("%s: %.2f", "Elapsed time in decimal", t.getDecimalTime());

   }    
}

但是,它可以正确计算某些时间,但是如果我输入例如0150和0240,则差值应该是50分钟,但它显示90,并且我需要使其不超过60,并将剩余时间转换为小时和分钟。而如果我输入一些其他数字,它就会起作用。任何帮助都是非常感谢的。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52766805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档