Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果语句未正确“读取”?_Java_If Statement - Fatal编程技术网

Java 如果语句未正确“读取”?

Java 如果语句未正确“读取”?,java,if-statement,Java,If Statement,我正在创建一个将标准时间转换为24小时时钟的程序。不幸的是,当用户输入错误的时间时,我的if语句似乎不能正常工作 这是我的代码,我已经包含了所有代码,因为我觉得这个错误可能是由另一行较低的代码引起的: public class StandardTime { static String amPM;//will hold am/pm for standard time static String traditionalTime;//will store traditional t

我正在创建一个将标准时间转换为24小时时钟的程序。不幸的是,当用户输入错误的时间时,我的if语句似乎不能正常工作

这是我的代码,我已经包含了所有代码,因为我觉得这个错误可能是由另一行较低的代码引起的:

public class StandardTime {

    static String amPM;//will hold am/pm for standard time 
    static String traditionalTime;//will store traditional time from user
    static int hours;//will store hours and minutes
    static int min1, min2;

    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// user input

        int tryAgain = 1;//will initial do-while loop
        System.out.println("Standard Time to Traditional Time Converter");
        System.out.println("===========================================");
        do {
            System.out.println();
            System.out.println("Input a time in Standard Form (HH:MM:SS):");
            String standardTime = br.readLine();//user inputs time in standard form
            System.out.println();
            String minute1 = standardTime.substring(3);
            min1 = Integer.parseInt(minute1);
            String minute2 = standardTime.substring(5);
            min2 = Integer.parseInt(minute2);
            do {
                if (standardTime.length() != 8) {
                    System.out.println("Invalid time entered.");
                    System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
                    standardTime = br.readLine();//user inputs time in standard form
                    System.out.println();
                }
                else if (min1 >= 6)
                {
                    System.out.println("Invalid time entered. Please do not input a minute time over 59.");
                    System.out.println("Input a time in Standard Form that has this form HH:MM:SS ...");
                    standardTime = br.readLine();//user inputs time in standard form
                    System.out.println();
                }
            } while ((standardTime.length()) != 8 || (min1 >= 6));
            //method declaration
            convertToTraditional(standardTime); // call the coversion method
            System.out.println(standardTime + " is equivalent to " + hours + ":" +min1+min2+ " " + amPM + ".");
            System.out.println();
            System.out.println("Enter 1 to try again or any other number to exit the program.");
            tryAgain = Integer.parseInt(br.readLine());//user decides to try again
        } while (tryAgain == 1);//will repeat if user enters 1

    }//closes main body

    public static void convertToTraditional(String standardTime) {
        String hour = standardTime.substring(0, 2); //captures substring of hours in user's input
        hours = Integer.parseInt(hour); //converts substring to INT value
        int pmHour = hours;
        if (12 == hours) { //creates a statement for 12PM (noon)
            hours = pmHour;
            amPM = "PM";
        } else if (hours > 12 && hours <= 23) { //if the time is not equal to noon or midnight
            hours = hours - 12;
            amPM = "PM";
        } else if (hours == 0) { //12 midnight
            hours = hours + 12;
            amPM = "AM";
        } else if (hours == 24) { //also another way to express 12 midnight on the 24 hour clock
            hours = hours - 12;
            amPM = "AM";
        } else { //else if the hours are between midnight and 11am
            hours = pmHour;
            amPM = "AM";
        }
    }
}

谢谢你的帮助

当您尝试将3解析为一个数字时会出现错误:当它带有冒号时,它就不是一个数字,它会抛出此异常


为什么不尝试使用java.util.Date呢?如果您使用它,您的代码将更干净、更易于编写

您的问题不是提到的if语句,而是以下代码:

String standardTime = br.readLine();//user inputs time in standard form
System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);
您正在从用户处读取输入并立即使用它,因此在检查输入的长度之前

在上述行之间移动do/while循环以修复该问题:

String standardTime = br.readLine();//user inputs time in standard form

do {
    // check
} while ((standardTime.length()) != 8 || (min1 >= 6));

System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);

看起来更像是一个ParseInt正在抛出。在检查standardTime.length之后,是否应该执行子字符串和parseInt位?改为执行01:02:03作为输入。它应该严格按照你的指示。此外,在尝试将字符串转换为int之前,还要解析出:此外,您有许多重复的代码,并执行许多Java可以为您完成的解析。也许需要重新思考?@Biffen这也可能是一个家庭作业,他/她不允许使用Date类或任何其他类。这是一个家庭作业,不允许使用Date类。谢谢你的观点。我要重写一下。
String standardTime = br.readLine();//user inputs time in standard form

do {
    // check
} while ((standardTime.length()) != 8 || (min1 >= 6));

System.out.println();
String minute1 = standardTime.substring(3);
min1 = Integer.parseInt(minute1);
String minute2 = standardTime.substring(5);
min2 = Integer.parseInt(minute2);