- public class Test1 {
-
- public static void main(String[] args)
- {
- //获取键盘录入对象
- Scanner in = new Scanner(System.in);
-
- System.out.println("please enter a number (-1 to stop):");
-
- int num=0;
- try
- {
- //循环录入 当用户输入-1后循环终止
- while((num=in.nextInt())!= -1)
- {
- //求出秒数以及总共有多少分钟
- int seconds = num%60;
- num = num/60;
-
- //求出分钟数以及总共有多少小时
- int minutes = num%60;
- num = num/60;
-
- //求出小时数以及总共有多少天
- int hours = num%24;
- num = num/24;
-
- int days = num;
- showTime(seconds,minutes,hours,days);
-
- }
- }
- catch(Exception e)
- {
- System.out.println("输入数据的格式错误");
- }
-
- }
-
- //显示时间
- private static void showTime(int...arr)
- {
- String[] strs = new String[]{"秒","分","时","天"};
-
- //建立缓冲区存放表示时间的字符串
- StringBuilder sb = new StringBuilder();
-
- for(int i=0;i<arr.length;i++)
- {
- //若a[i]不为0则将会打印相应值以及对应单位
- sb.insert(0,(arr[i]!=0 ? arr[i]+strs[i]:""));
- }
- System.out.println(sb.toString());
- }
- }
复制代码 |