首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >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

回答 3

Stack Overflow用户

发布于 2018-10-12 03:19:57

因此,要计算出两个24小时之间经过的时间,您需要转换为总分钟数,现在您正在使用的模函数不会像1小时= 60分钟那样工作,而不是100分钟。

因此,首先将所有小时/分钟转换为分钟时间。

代码语言:javascript
复制
int ftminuets = 0;
int stminuets = 0;
int totalmin = 0;
int elapsed = = 0
while(fTime >= 100)
{
     ftminuets += 60;
     fTime -= 100;
}
ftminuets += fTime; //gets remaining minuets from fTime.

while(sTime >= 100)
{
    stminuets += 60;
    sTime -= 100;
}
stminuets += sTime;  //gets remaining minuets from sTime.

//time to subtract
totalmin = stminuets - ftminuets;
//now total min has the total minuets in what can be considered to be 60 min increments.  Now just to switch it back into a 24 hours time.

while(totalmin >= 60)
{
    elapsed += 100;
    totalmin -= 60;
}
elapsed += totalmin; //get rest of the minuets.

现在,经过的时间应该是24小时内经过的时间。

票数 3
EN

Stack Overflow用户

发布于 2018-10-18 20:39:48

好了,这就是你正在寻找的解决方案。拆分mil.time,这样你就可以分别得到小时和分钟,然后剩下的就是休眠了。只有一件事你会接受这两个参数作为字符串。

代码语言:javascript
复制
public class TimeInterval {
    private String fTime;
    private String sTime;
    private static int timeDiffMinutes;

    public static void main(String[] args) {
        String fTime = "0330";
        String sTime = "1330";
        TimeInterval t = new TimeInterval(fTime, sTime);

        System.out.println("timeDiff  " + getTimeDiffMinutes());

        System.out.println(t.getHours() + "...." + t.getMinutes());
        System.out.println(t.getDecimalTime());

    }

    public TimeInterval(String fTime, String sTime) {
        int fTimeInt = Integer.valueOf(fTime);
        int sTimeInt = Integer.valueOf(sTime);

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

    public static int getTimeDiffMinutes() {
        return timeDiffMinutes;
    }

    public int getHours() {
        return Math.abs(timeDiffMinutes / 60);
    }

    public int getMinutes() {
        return Math.abs(timeDiffMinutes);
    }

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

    public void getTimeDiff() {
        final int mid1 = fTime.length() / 2; //get the middle of the String
        String[] parts = {fTime.substring(0, mid1), fTime.substring(mid1)};
        String fPart = parts[0];

        final int mid = sTime.length() / 2; //get the middle of the String
        String[] sParts = {sTime.substring(0, mid), sTime.substring(mid)};
        String sPart = sParts[0];

        int toBeExcluded = (Integer.valueOf(sPart) - Integer.valueOf(fPart)) * 40;
        this.timeDiffMinutes = (Integer.valueOf(sTime) - Integer.valueOf(fTime)) - toBeExcluded;
    }
}
票数 2
EN

Stack Overflow用户

发布于 2018-10-20 21:12:56

如何检查错误的无效条目?这是不必要的吗?如果是这样的话,你的方法即使目前只需要不到30个操作,也会增长到much2更多的操作,也就是不包括0000小时或2400小时的问题。

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

https://stackoverflow.com/questions/52766805

复制
相关文章

相似问题

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