用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/04/29 04:28:50
用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: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 {
}

用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写一个程序,求N个不同形状的图形的总面积. 如何编写一个打印50~100随机数的简单的程序用java写 帮忙看一下,这句英文怎么写?我用java语言编写了一个程序,名字叫myApp,我英文标注一下,证明软件是java编写的,应该是myApp for java 还是myApp by java 还是其他写法? 用 Java 写一个程序,打印所有的大写英文字母,和它们所对应的 ASCII 码. 用java正则式如何把一个文件里的某些对应的标签找出来,并将标签里的内容提取出来.文件内容如下:........任务是要把和标签里的内容提取到另外一个文件中.能大概写一个java程序吗? 用java语言写一个程序要求:随机生成两个二维数组,计算这两个矩阵的乘法.能写函数尽量多写函数,速求! 英语翻译本文阐述了使用JAVA编程语言对基于客户/服务器模式的应用编写网络通信程序,讨论了SOCKET机制、输入输出流以及程序实现代码.java它一个工具,一个用纯java语言写的跨平台的一个通用 初学java,写了一个链接Oracle的小程序,运行后报错:java.sql.SQLException:无效的列索引请问这是为什么?附上程序:package connection;import java.sql.*;public class Connectionb { public static final void main(String args[] 用JAVA写一个将华氏温度转换成摄氏温度的程序程序的输入是一个整数,表示华氏温度.输出对应的摄氏温度,也是一个整数.提示,为了把计算结果的浮点数转换成整数,需要使用下面的表达式:(in 编写程序,画出函数f(x)=x*x的图像用java如何写? 用JAVA程序冒泡排序算法把一组打乱顺序的数字从小到大排列并打印出来用冒泡排序算法把一组打乱顺序的数字从小到大排列并打印出来,要求用JAVA程序写,没学过程序的求大哥帮助啊,最简单 JAVA题目 :随机生成100个1000以内的整数,将这100个整数中的素数找出来并写到一个文本文件中.急 用最简单的JAVA程序来编程 ,容易看的懂的 写一个完整的程序,计算1到150的奇数之和 ,偶数之和并输出运算结果 用java感激不尽 用java编写一个输出1到00以内的素数并求和的程序 用java语言写:已知圆球体积为4/3*PI*r3,试编写一个程序,要求输入圆球半径,经过计算输出圆球的体积 编写一个程序 ,采用方法重载分别实现圆、三角形、矩形、椭圆的面积计算 是用java程序哦 用Java编写一个程序可以打印如下三角形一定要有详细的说明才行!