- mport java.util.Scanner;
- public class ShowTime {
- public static void main(String[] args) {
- int time = getInt();
- outTime(time);
- }
-
- /**
- * 获取从控制台输入数字
- * @param name
- * @return
- */
- private static int getInt() {
- boolean flog = true;
- int result = 0;
- while (flog){
- Scanner sc = new Scanner(System.in);
- System.out.println("输入一个大于0的整数:");
- try{
- result = Integer.parseInt(sc.nextLine());
- if (result < 0) {
- throw new Exception("输入的值必须大于等于0");
- }
- flog = false;
- } catch(Exception e) {
- System.out.println("输入值非法,请输入大于0的整数!");
- }
- }
- return result;
- }
-
- private static void outTime(int time){
- int s = 0, m = 0, h = 0, d = 0;
- if (time < 0) return ;
- if (time < 60) {
- s = time;
- } else if (time >= 60 && time < 60*60 ){
- m = time/60;
- s = time%60;
- } else if (time >= 60*60 && time < 24*60*60){
- h = time/(60*60);
- m = time%(60*60)/60;
- s = time%(60*60)%60;
- } else {
- d = time/(24*60*60);
- h = time%(24*60*60)/(60*60);
- m = time%(24*60*60)%(60*60)/60;
- s = time%(24*60*60)%(60*60)%60;
- }
- out(d, h, m, s);
- }
-
- private static void out(int d, int h, int m, int s) {
- String str = "";
- if (d != 0) {
- str += d + "天";
- }
- if (h != 0) {
- str += h + "小时";
- }
- if (m != 0) {
- str += m + "分钟";
- }
- if (s != 0) {
- str += s + "秒";
- }
- System.out.println(str);
- }
- }
复制代码
|