灶晌 private static void print(int h,int m,int s){
int ss = h * 60 * 60 + m * 60 + s
System.out.format("%d小时%d分%d秒等于%d秒%n",h,m,s,ss)
}
private static void print(int s){
int h = s/60/60
int m 饥掘= s/60%60
烂辩核 int ss = s%60
System.out.format("%d秒等于%d小时%d分%d秒%n", s,h,m,ss)
}
public static void main(String[] args){
print(1,28,42)
print(9999)
}
}
import java.util.Scannerpublic 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)