本帖最后由 yanzhendong 于 2014-4-14 16:10 编辑
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class Time {
- public static void main(String[] args) throws Exception {
- int time;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- while (true) {
- System.out.println("请输入一个整数我将把它格式化:");
- time = Integer.parseInt(br.readLine());
- System.out.println(timeFomat(time));
-
- }
- }
- static String timeFomat(int time) {
- int day, hour, minute, second, temp = time;
- second = temp % 60;
- temp = temp / 60;
- minute = temp % 60;
- temp = temp / 60;
- hour = temp % 24;
- day = temp / 24;
- return (day == 0 ? "" : day + "天")
- +(hour == 0 ? "" : hour + "小时")
- +(minute == 0 ? "" : minute + "分钟")
- +(second == 0 ? "" : second + "秒");
- }
- }
复制代码 给你优化后的代码
|