Java 中的分支和循环(中断并继续)

Branching and looping in Java (break and 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
        }
    }
}

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

程序中有两个错误。

1.Before 解析成整数验证用户输入是否有效 使用正则表达式。如果匹配 returns false 抛出异常。

String s="235:23";//user input
System.out.println(s.matches("\d{0,2}:\d{0,2}"));

2.Instead of equalsIgnoreCase 或 equals,您正在使用 == 比较字符串

 if( "y".equalsIgnoreCase(newyn) || "n".equalsIgnoreCase(newyn))

请遵循一些编码标准!

实际上在您的代码中您没有正确比较字符串,如果用户在询问时输入的不是 Y/N 并且按时输入错误,您也没有处理您的代码。见以下代码:

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
        }
    }
}