用java写一个程序把24小时制的时间转换为12小时制的时间.具体说明内详下面是示例对话过程:Enter time in 24-hour notation:13:07That is the same as1:07 PMAgain?(y/n)yEnter time in 24-hour notation:10:15That is the same as
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/02 20:01:04
![用java写一个程序把24小时制的时间转换为12小时制的时间.具体说明内详下面是示例对话过程:Enter time in 24-hour notation:13:07That is the same as1:07 PMAgain?(y/n)yEnter time in 24-hour notation:10:15That is the same as](/uploads/image/z/417608-8-8.jpg?t=%E7%94%A8java%E5%86%99%E4%B8%80%E4%B8%AA%E7%A8%8B%E5%BA%8F%E6%8A%8A24%E5%B0%8F%E6%97%B6%E5%88%B6%E7%9A%84%E6%97%B6%E9%97%B4%E8%BD%AC%E6%8D%A2%E4%B8%BA12%E5%B0%8F%E6%97%B6%E5%88%B6%E7%9A%84%E6%97%B6%E9%97%B4.%E5%85%B7%E4%BD%93%E8%AF%B4%E6%98%8E%E5%86%85%E8%AF%A6%E4%B8%8B%E9%9D%A2%E6%98%AF%E7%A4%BA%E4%BE%8B%E5%AF%B9%E8%AF%9D%E8%BF%87%E7%A8%8B%3AEnter+time+in+24-hour+notation%3A13%3A07That+is+the+same+as1%3A07+PMAgain%3F%28y%2Fn%29yEnter+time+in+24-hour+notation%3A10%3A15That+is+the+same+as)
用java写一个程序把24小时制的时间转换为12小时制的时间.具体说明内详下面是示例对话过程:Enter time in 24-hour notation:13:07That is the same as1:07 PMAgain?(y/n)yEnter time in 24-hour notation:10:15That is the same as
用java写一个程序把24小时制的时间转换为12小时制的时间.具体说明内详
下面是示例对话过程:
Enter time in 24-hour notation:
13:07
That is the same as
1:07 PM
Again?(y/n)
y
Enter time in 24-hour notation:
10:15
That is the same as
10:15 AM
Again(y/n)
y
Enter time in 24-hour notation:
10:65
There is no such time as 10:65
Try again:
Enter time in 24-hour notation:
16:05
That is the same as
4:05 PM
Again?(y/n)
n
End of program
要定义一个名为TimeFormatException的异常类.如果用户输入了不合法的时间,比如10:65,甚至是无意义的东西,比如8&*68,程序将会抛出、捕获并处理一个TimeFormatException
用java写一个程序把24小时制的时间转换为12小时制的时间.具体说明内详下面是示例对话过程:Enter time in 24-hour notation:13:07That is the same as1:07 PMAgain?(y/n)yEnter time in 24-hour notation:10:15That is the same as
你看一下,这个是不是你想要的,
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
while (true) {
System.out.println("Enter time in 24-hour notation:");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
try {
outTime(line);
} catch (TimeFormatException e) {
System.out.println("There is no such time as " + line);
System.out.println("Try again:");
continue;
}
sc = new Scanner(System.in);
line = sc.nextLine();
if ("n".equalsIgnoreCase(line)) {
break;
}
}
System.out.println("End of program");
}
public static void outTime(String line) throws TimeFormatException {
SimpleDateFormat _24time = new SimpleDateFormat("HH:mm");
SimpleDateFormat _12time = new SimpleDateFormat("hh:mm a",
Locale.ENGLISH);
try {
String[] array = line.split(":");
if (Integer.parseInt(array[0]) < 0
|| Integer.parseInt(array[0]) > 23) {
throw new TimeFormatException();
}
if (Integer.parseInt(array[1]) < 0
|| Integer.parseInt(array[1]) > 59) {
throw new TimeFormatException();
}
System.out.println(_12time.format(_24time.parse(line)));
System.out.println("Again?(y/n)");
} catch (Exception e) {
throw new TimeFormatException();
}
}
}
class TimeFormatException extends Exception {
}