这是我的代码
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* 第二题 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如: 键盘输入6,打印6秒; 键盘输入60,打印1分; 键盘输入66,打印1分6秒;
* 键盘输入666,打印11分6秒; 键盘输入3601,打印1小时1秒
*
*/
public class Test2 {
@SuppressWarnings("resource")
public static void main(String args[]) throws Exception {
// 从键盘输入一个整数
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数");
int number = 0;
try {// 从键盘输入一个整数
number = scanner.nextInt();
// 将接收到的数据除以相应的数值,换算成相应的天、时、分、秒
int day = number / 3600 / 24;
int hours = (number - 3600 * 24 * day) / 3600;
int second = (number - 3600 * 24 * day - hours * 3600) / 60;
int minute = (number - 3600 * 24 * day - hours * 3600 - second * 60);
// 判断天、时、分、秒是否大于0,大于0则输出
if (day > 0)
System.out.print(day + "天");
if (hours > 0)
System.out.print(hours + "时");
if (second > 0)
System.out.print(second + "分");
if (minute > 0)
System.out.print(minute + "秒");
} catch (InputMismatchException e) {
// 抛出相应的输入数据类型异常
throw new InputMismatchException("请输入一个整数");
}
scanner.close();
}
}
|