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

时间:2015-10-10 18:21:50

标签: java loops exception-handling 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
        }
    }
}

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

2 个答案:

答案 0 :(得分:0)

程序中有两个错误。

1.在解析为整数验证之前,用户输入是否为有效数字  使用正则表达式。如果匹配返回false抛出异常。

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

2.而不是equalsIgnoreCase或equals,你使用==来比较字符串

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

请遵循一些编码标准!

答案 1 :(得分:0)

实际上,在您的代码中,您没有正确地比较字符串以及如果用户在询问时输入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
        }
    }
}