用java写一个程序把24小时制的时间转换为12小时制的时间.具体说明内详
下面是示例对话过程:Entertimein24-hournotation:13:07Thatisthesameas1:07PMAgain?(y/n)yEntertimei...
下面是示例对话过程:
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
以上就是要求,希望哪位大神能帮忙解决一下. 展开
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
以上就是要求,希望哪位大神能帮忙解决一下. 展开
4个回答
展开全部
import java.util.Scanner;
public class TimeFormatException extends Exception {
public void printException() {
System.out.println("输入时间错误!!程序结束");
}
public TimeFormatException() {
}
public void printDate() throws TimeFormatException {
boolean bStop = true;
Scanner input = new Scanner(System.in);
String reg = "([0-1][0-9]|2[0-4]):([0-5][0-9])";
while (bStop) {
System.out.println("请输入时间:");
String str = input.next();
if (str.matches(reg)) {
int hour = Integer.parseInt(str.substring(0, 2));
String minutes = str.substring(2, 5);
if (hour < 12)
System.out.println("现在时间是:" + Integer.toString(hour).concat(minutes) + " am");
else if (hour == 12)
System.out.println("现在时间是:" + Integer.toString(hour).concat(minutes) + " pm");
else if(hour == 24)
System.out.println("现在时间是:" + "00".concat(minutes) + " am");
else
System.out.println("现在时间是:" + Integer.toString(hour - 12).concat(minutes) + " pm");
} else {
bStop = false;
throw new TimeFormatException();
}
}
}
public static void main(String[] args) {
try {
new TimeFormatException().printDate();
} catch (TimeFormatException e) {
e.printException();
}
}
}
如果看不懂 尽管问 ch_felix168_88@163.com
展开全部
用String.splt(":");分成两部分,然后判断小时是否在0-24小时范围内,分钟也是。就这样。
追问
java基础不是很好,能麻烦你把整个代码详细写出来一下吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你看一下,这个是不是你想要的,
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 {
}
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 {
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
实现这个的作用?
追问
书上的一个习题,我看了书上关于异常的内容,始终还是不明白这里如何去捕获这种异常,以及如何判断时间格式的正确与否.希望能有一个模版看看.应该就更容易明白一点了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询