【发布时间】:2017-04-02 03:16:36
【问题描述】:
当我输出以下代码时(取自梁博士 Java 简介,第 10 版,第 3 章 - 选择)
/*
(Current time) Listing 2.7, ShowCurrentTime.java, gives a program that displays
the current time in GMT. Revise the program so that it prompts the user to enter
the time zone offset to GMT and displays the time in the specified time zone.
*/
import java.util.Scanner;
public class Ex_03_08 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the time zone (GMT): ");
int gmt = input.nextInt();
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;
currentHour = currentHour + gmt;
System.out.println("The current time is " + currentHour + ":"
+ currentMinute + ":" + currentSecond);
input.close();
}
}
输出是
Enter the time zone (GMT): 1
The current time is 11:2:31
我怎样才能让显示代替11:02:31?
谢谢。
【问题讨论】: