【问题标题】:Branching and looping in Java (break and continue)Java 中的分支和循环(中断和继续)
【发布时间】:2015-10-10 18:21:50
【问题描述】:

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

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 的异常类。

【问题讨论】:

标签: java loops exception-handling break continue


【解决方案1】:

实际上,在您的代码中,如果用户在询问时输入的不是 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
        }
    }
}

【讨论】:

    【解决方案2】:

    程序有两个错误。

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

    请遵守一些编码标准!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-05
      • 2020-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多