题目
读入非负整数总秒数,按 时:分:秒 格式输出(时分秒均为整数,可不带前导零)。
思路
本题要求:把总秒数换算成「时:分:秒」形式的整数。 设总秒数为 s:小时 h = s / 3600;去掉整小时后,分钟 m = (s % 3600) / 60;秒仍是 s % 60。最后按题目要求的格式用 System.out 输出(是否补零看题面)。
解题分析
总秒数先取小时 /3600,余数再分、秒。注意余数用 %3600 再 /60 和 %60,不要对原 total 重复除错次序。
完整程序
import java.io.*;
public class Main {
public static void main(String[] args) {
int total = 3661;
int h = total / 3600;
int m = (total % 3600) / 60;
int s = total % 60;
System.out.println(h + ":" + m + ":" + s);
}
}
运行示例
输出:
1:1:1