楼主,这个是基础测试题还是入学测试题啊、?我也遇到了,分享下我的程序。。
- import java.io.*;
- public class Test2 {
- /**
- * 2.从键盘接受一个数字,打印该数字表示的时间,最大单位到天
- *
- */
- public static void main(String[] args) {
- //从键盘读取数据
- BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
- int num = 0;
- try {
- num = Integer.parseInt(in.readLine());
- } catch (NumberFormatException e) {
- System.out.println("please in put number!");
- e.printStackTrace();
- } catch (IOException e) {
- System.out.println("read wrong!");
- e.printStackTrace();
- }
- calculateTime(num);
- }
-
- //计算时间 并打印
- public static void calculateTime(int num){
- int day = 0, hour = 0, minute = 0, second = 0;
-
- second = num % 60;
- minute = (num / 60) % 60;
- hour = ((num / 60) / 60) % 24;
- day = ((num / 60) / 60) / 24;
-
- if(day > 0)
- System.out.print(day+"天");
- if(hour > 0)
- System.out.print(hour+"小时");
- if(minute > 0)
- System.out.print(minute+"分");
- if(second >0)
- System.out.print(second+"秒");
- }
- }
复制代码
|