转换时间到当前时间

转换时间到当前时间

问题描述:

我有一个开始时间(在这种情况下是9点),并且有一定数量的时间被添加到它,然后需要计算当前时间。我正在使用的当前方法是将传递到原始时间的小时数加起来,然后用12来模数,如下所示:timeAndHoursPassed % 12 = currentTime在所有情况下都能很好地工作,除了添加时间可以被12整除时,在这种情况下,时间是零而不是12.我该如何解决这个问题?另外,如果可能的话,我宁愿尽可能使用一些基本的数学运算,而不是使用GregorianCalender类。 感谢您提前提供任何帮助。 我的代码如下:转换时间到当前时间

package week11; 

import java.util.Scanner; 

public class PassingTrains { 
    static int currentTime = 9, firstDistance = 0, secondDistance = 0, firstSpeed = 40, secondSpeed; 
    static Scanner input = new Scanner(System.in); 
    public static String passTime(){ 
     System.out.print("Enter the speed of the passenger train: "); 
     secondSpeed = input.nextInt(); 
     while (currentTime < 11) { 
      firstDistance += firstSpeed; 
      currentTime++; 
     } 
     while (firstDistance > secondDistance) { 
      firstDistance += firstSpeed; 
      secondDistance += secondSpeed; 
      currentTime++; 
     } 
     if (firstDistance == secondDistance){ 
      return ("at " + currentTime % 12); 
     } else { 
      return ("between " + (currentTime - 1) % 12 + " o'clock and " + currentTime % 12); 
     } 
    } 

    public static void main(String[] args) { 
      System.out.println("The passenger train passed the freight train at " + passTime() + " o'clock"); 
      System.out.println("The freight train was traveling " + firstSpeed + " mph"); 
      System.out.println("The passenger train was traveling at " + secondSpeed + " mph"); 
    } 
} 
+0

首先,发布你的代码。 – hfontanez 2014-11-09 01:33:26

+0

@JasonC该建议不起作用。 – hfontanez 2014-11-09 01:56:34

+1

@hfontanez后编辑,不,它不。感谢ping。 – 2014-11-09 01:59:17

有两个部分:

  1. 你有模24适用于经过的时间。为什么。如果你今天9点开始,并且24小时过后,你增加零小时(24%24),你有第二天9点。
  2. 让我们假设37已经过去了。申请#1以上,你有超过24小时13小时。加13到9,你有22个小时(下午10点)。如果你不希望使用24小时日历,从当前时间减去12 IF当前小时> 12

示例代码:

currentTime += elapsedTime % 24; 
if(currentTime > 12) 
{ 
    currentTime -= 12; 
} 

UPDATE:OP的修正码

public class PassingTrains 
{ 
    static int currentTime = 9, firstDistance = 0, secondDistance = 0, 
      firstSpeed = 40, secondSpeed, elapsedTime; 
    static Scanner input = new Scanner(System.in); 

    public static String passTime() 
    { 
     System.out.print("Enter the speed of the passenger train: "); 
     secondSpeed = input.nextInt(); 
     while (currentTime < 11) 
     { 
      firstDistance += firstSpeed; 
      currentTime++; 
     } 
     while (firstDistance > secondDistance) 
     { 
      firstDistance += firstSpeed; 
      secondDistance += secondSpeed; 
      elapsedTime++; // changed this 

     } 
     // added the next two lines 
     currentTime += elapsedTime % 24; 
     currentTime = (currentTime > 12) ? currentTime - 12 : currentTime; 

     if (firstDistance == secondDistance) 
     { 
      return ("at " + currentTime); // fixed this 
     } 
     else 
     { 
      return ("between " + ((currentTime - 1 == 0) ? 12 : currentTime - 1) + " o'clock and " + currentTime); \\fixed this 
     } 
    } 

    public static void main(String[] args) 
    { 
     System.out.println("The passenger train passed the freight train at " 
       + passTime() + " o'clock"); 
     System.out.println("The freight train was traveling " + firstSpeed 
       + " mph"); 
     System.out.println("The passenger train was traveling at " 
       + secondSpeed + " mph"); 
    } 
}