这是输出:
时间是上午 8:3:8 时间是 8:3:8
这是所需的输出:
时间是 08:03:08 AM 时间是 08:03:08
因此,我需要将其转换为所需的输出,如果是 8:03:04,则应该像上面那样显示 8 小时 3 分 4 秒。个位数前面有零
package Assignment2;
class clock{
public static void main(String[] args) {
clock timer = new clock();
timer.setTime(8,02,59);
timer.incTime(9);
//timer.incTime(60);
timer.display12hr();
timer.display24hr();
}
private int minutes, hours, seconds;
clock()
{
minutes = 0;
hours = 0;
seconds = 0;
}
clock(int x, int y, int z)
{
hours = x;
minutes = y;
seconds = z;
}
void setTime(int x, int y, int z)
{
hours = x;
minutes = y;
seconds = z;
}
void incTime(int y)
{
if (seconds + y > 59)
{
seconds = (seconds + y) - 60;
minutes++;
}
else
seconds += y;
}
void display12hr()
{
if (hours > 12)
{
System.out.println("The time is " + (hours - 12) + ":" + minutes + ":" + seconds
+ "PM");
}
else
{
System.out.println("The time is " + (hours) + ":" + minutes + ":" + seconds +"AM");
}
}
void display24hr()
{
System.out.println("The time is " + (hours) + ":" + minutes + ":" + seconds);
}
}