我必须以 DDMMYYYY 格式拆分用户输入的日期。但是,如果您输入 70702000,它会显示我的所有 3 个错误,而不是一个

I have to split a date inputted by a user in the format DDMMYYYY. however if you input 70702000 its displaying all 3 of my errors instead of one

代码下方:

    {
        System.out.println("Please enter the date you would like to split");
        System.out.println("Please make sure it is in the format DDMMYYYY");
        date = sc.nextInt();
        
        day = date / 1000000;
        month = (date / 10000) - (day * 100);
        year = date - (date / 10000) * (10000);
        
        switch(day) 
        {
            case 1: case 21: case 31: 
                suffix = "st";
            break;

            case 2: case 22:
                suffix = "nd";
            break;

            case 3: case 23: 
                suffix = "rd";
            break;

            case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 24: case 25: case 26: case 27: case 28: case 29: case 30:
                suffix = "th";
            break;
            
            default:
                System.out.println("Error, Please enter a valid day (i.e. DD) that is between 01 - 31");
            break;
        }
        
        switch(month)
        {
            case 4: case 6: case 9: case 11: 
                if ((day < 1) || (day > 30))
                {
                    System.out.println("Error this day does not exist"); 
                }
            break;

            case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
                if ((day < 1) || (day > 31))
                {
                    System.out.println("Error this day does not exist");
                }
            break;

            case 2:
                if ((day < 1) || (day > 28))
                {
                    if ((day != 29)) 
                    {
                        System.out.println("Error this day does not exist");
                    }
                    else if ((year % 4 != 0) || ((year % 400 != 0) && (year % 100 == 0)))
                    {
                        System.out.println("Error this day does not exist");
                    }
                        //If it isn't a leap year, febuary cannot have 29 days
                }
            break;
            default:
                System.out.println("Error this day does not exist");
        }

        switch(month)
        {
            case 1:
                monthName = "January";
            break;
            case 2:
                monthName = "Febuary";
            break;
            case 3: 
                monthName = "March";
            break;
            case 4:
                monthName = "April";
            break;
            case 5: 
                monthName = "May";
            break; 
            case 6: 
                monthName = "June";
            break;
            case 7:
                monthName = "July";
            break;
            case 8:
                monthName = "August";
            break;
            case 9: 
                monthName = "September";
            break; 
            case 10:
                monthName = "October";
            break;
            case 11: 
                monthName = "November";
            break;
            case 12:
                monthName = "December";
            default:
                System.out.println("Error, Please make sure the month (i.e. MM) is between 01 and 12");
            break;
        }
        
        if ((day == 29) && (month == 2))
        {
            System.out.println("It is the 29th day of Febuary in " + year + ".");
        }
        else 
        {
            System.out.println("It is the " + day + suffix + " day of " + monthName + " in " + year + ".");
        }
    }

所以,基本上当我 运行 这个程序,并输入 70702020 值时,我会得到以下所有输出:

Error, Please enter a valid day (i.e. DD) that is between 01 - 31

Error this day does not exist

Error, Please make sure the month (i.e. MM) is between 01 and 12

然后它将输出最终输出(这仅在用户输入有效日期时才有用)

建议先看评论,再自己执行代码。 我添加一些评论。

public class TestFormatDate{

     public static void main(String []args){
        System.out.println("Please enter the date you would like to split");
        System.out.println("Please make sure it is in the format DDMMYYYY");

        //your input:70702020
        int date = 70702020;

        //70
        int day = date / 1000000;
        //70
        int month = (date / 10000) - (day * 100);
        //2020
        int year = date - (date / 10000) * (10000);
        String suffix = null;
        String monthName = null;
        switch(day) 
        {
            case 1: case 21: case 31: 
                suffix = "st";
            break;

            case 2: case 22:
                suffix = "nd";
            break;

            case 3: case 23: 
                suffix = "rd";
            break;

            case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 24: case 25: case 26: case 27: case 28: case 29: case 30:
                suffix = "th";
            break;
            // day is 70, so the default is executed.
            default:
                System.out.println("Error, Please enter a valid day (i.e. DD) that is between 01 - 31");
            break;
        }

        switch(month)
        {
            case 4: case 6: case 9: case 11: 
                if ((day < 1) || (day > 30))
                {
                    System.out.println("Error this day does not exist"); 
                }
            break;

            case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
                if ((day < 1) || (day > 31))
                {
                    System.out.println("Error this day does not exist");
                }
            break;

            case 2:
                if ((day < 1) || (day > 28))
                {
                    if ((day != 29)) 
                    {
                        System.out.println("Error this day does not exist");
                    }
                    else if ((year % 4 != 0) || ((year % 400 != 0) && (year % 100 == 0)))
                    {
                        System.out.println("Error this day does not exist");
                    }
                        //If it isn't a leap year, febuary cannot have 29 days
                }
            break;
            // month is 70, so the default is executed.
            default:
                System.out.println("Error this day does not exist");
        }

        switch(month)
        {
            case 1:
                monthName = "January";
            break;
            case 2:
                monthName = "Febuary";
            break;
            case 3: 
                monthName = "March";
            break;
            case 4:
                monthName = "April";
            break;
            case 5: 
                monthName = "May";
            break; 
            case 6: 
                monthName = "June";
            break;
            case 7:
                monthName = "July";
            break;
            case 8:
                monthName = "August";
            break;
            case 9: 
                monthName = "September";
            break; 
            case 10:
                monthName = "October";
            break;
            case 11: 
                monthName = "November";
            break;
            case 12:
                monthName = "December";
            // month is 70, so the default is executed.
            default:
                System.out.println("Error, Please make sure the month (i.e. MM) is between 01 and 12");
            break;
        }

        if ((day == 29) && (month == 2))
        {
            System.out.println("It is the 29th day of Febuary in " + year + ".");
        }
        // day is 70, so the else is executed.
        else 
        {
            System.out.println("It is the " + day + suffix + " day of " + monthName + " in " + year + ".");
        }
     }
}

您正在使用 switch 而不是 if(blah blah) 语句。意思是,该陈述是真实的并且现在产生错误。您的代码表明您的输入没有任何问题。您的后缀仍会打印,但会打印空引号,因为我相信它是一个空字符串。我建议在使用开关之前检查入口。或者使用 "System.exit(1)" 如果输入不正确,系统将退出。

编辑:根据计算,您的一天也将等于“70”,因此会出现其他错误。

break 仅跳出 switch 语句

您的 switch 语句中的 break 命令仅从 switch 语句中退出,不是您的整个方法

您有一系列的三个 switch 语句。

  • 当您跳出第一个 switch 语句时,控制流将转到第二个 switch 语句。
  • 当您跳出第二个 switch 语句时,控制流转到第三个 switch 语句。
  • 当您跳出第三个 switch 语句时,控制流将移至其余语句。

编程的核心概念之一是scope. You must learn to think in terms of boxes nested within boxes, like the Russian nesting dolls。在 switch 语句中,break 命令的作用域只是一条语句,而不是整个方法。 switch 语句是嵌套在外部方法中的一层作用域。 switch语句中的break只能"see"其直接包含框:switch语句。

java.time

顺便说一句,通过现代 java.time 类 解析为文本 简单得多。

try {
    LocalDate ld = 
            LocalDate
            .parse( 
                "70702020" , 
                DateTimeFormatter.ofPattern( "ddMMuuuu" ) 
            )
    ;
} catch ( DateTimeParseException e ) {
    … 
}

无效输入抛出 DateTimeParseException