2014-11-09 121 views
-1

我有一個開始時間(在這種情況下是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

有兩個部分:

  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"); 
    } 
}