gpt4 book ai didi

java - Java 中的分支和循环(break 和 continue)

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:01 27 4
gpt4 key购买 nike

嗨,我正在尝试编写一个代码,其输出类似于

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

但是我最终犯了一些错误,而且我无法弄清楚。

public class prp {
public static void main(String[] args) {
while (true) //add the remaining logic
{
System.out.println("Enter time in 24-hour notation HH:MM");
Scanner x = new Scanner(System.in);
String newhr = x.nextLine();
String hr[] = newhr.split(":");
int hours = Integer.parseInt(hr[0]);//HH
int minutes = Integer.parseInt(hr[1]);//MM

if ((hours >= 00 && hours <= 24) && (minutes >= 00 && minutes <= 59)) {
System.out.println("That is the same as: ");
if (hours <= 12) {
System.out.println(hours + ":" + minutes + " AM");
//System.exit(0);
} else if (hours > 12 && hours < 24) {
int hoursnew = hours - 12;
System.out.println(hoursnew + ":" + minutes + " PM");
//System.exit(0);
}
} else {
System.out.println("There is no such time as " + hours + " : " + minutes);
System.out.println("Try Again!");
//continue;
}
System.out.println("Again? [y/n]");
Scanner y = new Scanner(System.in);
String newyn = y.nextLine();
if (newyn == "y" || newyn == "n") {
if (newyn == "y") {
continue;
} else {
System.out.println("End of program");
System.exit(0);
//break;
}
}//end of while
}
}
}

输入非整数时程序显示错误。此外,它并没有破裂。如果用户输入非法时间,例如 10:65 或 ab:cd,我想创建另一个名为 TimeFormatException 的异常类。

最佳答案

实际上,在您的代码中,您没有正确比较字符串,并且如果用户在询问时输入“是/否”以外的内容以及按时输入错误,您也不会处理您的代码。参见下面的代码:

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

public class Exam {
public static void main(String[] args) {
while (true) // add the remaining logic
{
System.out.println("Enter time in 24-hour notation HH:MM");
Scanner x = new Scanner(System.in);
String newhr = x.nextLine();
String hr[] = newhr.split(":");
int hours = 0;
int minutes = 0;
try {
hours = Integer.parseInt(hr[0]);// HH
minutes = Integer.parseInt(hr[1]);// MM
} catch (NumberFormatException e) {
System.out.println("Wrong time input");
continue;
}
if ((hours >= 00 && hours <= 24)
&& (minutes >= 00 && minutes <= 59)) {
System.out.println("That is the same as: ");
if (hours <= 12) {
System.out.println(hours + ":" + minutes + " AM");
// System.exit(0);
} else if (hours > 12 && hours < 24) {
int hoursnew = hours - 12;
System.out.println(hoursnew + ":" + minutes + " PM");
// System.exit(0);
}
} else {
System.out.println("There is no such time as " + hours + " : "
+ minutes);
System.out.println("Try Again!");
// continue;
}

while (true) {
System.out.println("Again? [y/n]");
Scanner y = new Scanner(System.in);
String newyn = y.nextLine();
if ("y".equalsIgnoreCase(newyn)) {
break;
} else if ("n".equalsIgnoreCase(newyn)) {
System.out.println("End of program");
System.exit(0);
// break;
} else {
System.out.println("Enter correct input Y or N");
}
}
// end of while
}
}
}

关于java - Java 中的分支和循环(break 和 continue),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33057451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com