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

标签 java loops exception break continue

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

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/

相关文章:

javascript - 访问动态创建的对象..js

python - 在 Python 中使用循环来配对索引

java - 您将如何拦截所有异常?

c# - 统一处理众多异常

c++ - 32 位工作正常,但 64 位出现错误 : Microsoft C++ exception: _com_error at memory location

java - Tomcat 连接器如何工作?

java - 如何在freemarker中检查空列表

c - 让 for 循环处理负数

java - 如何构建一个java项目?

java - 在java中查找x和y坐标是否在 View 内由两个相交的矩形包围