-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");
}
}
首先,發佈你的代碼。 – hfontanez 2014-11-09 01:33:26
@JasonC該建議不起作用。 – hfontanez 2014-11-09 01:56:34
@hfontanez後編輯,不,它不。感謝ping。 – 2014-11-09 01:59:17