私は、フォーマットDDMMYYYYでユーザーが入力した日付を分割する必要があります。しかし、もしあなたの入力70702000その私のすべてのエラー3を表示する代わりに、1

Lezcer:

コード下:

    {
        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価値を、私は以下のすべての出力を得ることができます:

31 - エラー、有効日01の間にある(すなわちDD)を入力してください。

この日は存在しないエラー

エラー、必ず月にしてください(つまりMM)が01と12の間にあります

そしてそれは、(ユーザーが有効な日付を入力した場合、これが唯一の意図していた)出力最終出力を希望

バジルボーク:

break 唯一のswitch文の勃発

breakあなたのswitch文のコマンドは、switch文のみの救済ではないあなたの全体の方法

あなたは3つのスイッチ一連の文を持っています。

  • あなたが最初にswitch文から抜け出すときは、制御の流れは、第二のswitch文に移ります。
  • 2番目のswitch文から抜け出すときは、制御の流れは、第三switch文に移ります。
  • あなたは第三switch文から抜け出すときは、制御の流れは、残りのステートメントに移ります。

java.time

ところで、現代を経由してテキストとして構文解析java.timeのクラスがあるくらいに簡単。

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

無効な入力が投げますDateTimeParseException

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=349984&siteId=1