2 u014101606 u014101606 于 2014.07.03 15:00 提问

Java对话框输入,修改下面代码,若输入不合法,抛出异常并重新输入

import javax.swing.*;

public class Calendar {
public static void main(String[] args) {
// Prompt the user to enter input
String yearInput = JOptionPane.showInputDialog("Enter a year:");
int year = Integer.parseInt(yearInput);

    String firstDayInput = JOptionPane
            .showInputDialog("Enter the first day of the year:");
    int firstDay = Integer.parseInt(firstDayInput);

    int startDay = firstDay;
    int numOfDaysInMonth = 0;

    for (int month = 1; month <= 12; month++) {
        System.out.print("          ");
        switch (month) {
        case 1:
            System.out.println("January/" + year);
            numOfDaysInMonth = 31;
            break;

        case 2:
            System.out.println("Feburay/" + year);
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                numOfDaysInMonth = 29;
            else
                numOfDaysInMonth = 28;
            break;

        case 3:
            System.out.println("March/" + year);
            numOfDaysInMonth = 31;
            break;

        case 4:
            System.out.println("April/" + year);
            numOfDaysInMonth = 30;
            break;

        case 5:
            System.out.println("May/" + year);
            numOfDaysInMonth = 31;
            break;

        case 6:
            System.out.println("June/" + year);
            numOfDaysInMonth = 30;
            break;

        case 7:
            System.out.println("July/" + year);
            numOfDaysInMonth = 31;
            break;

        case 8:
            System.out.println("August/" + year);
            numOfDaysInMonth = 31;
            break;

        case 9:
            System.out.println("September/" + year);
            numOfDaysInMonth = 30;
            break;

        case 10:
            System.out.println("October/" + year);
            numOfDaysInMonth = 31;
            break;

        case 11:
            System.out.println("November/" + year);
            numOfDaysInMonth = 30;
            break;

        case 12:
            System.out.println("December/" + year);
            numOfDaysInMonth = 31;
            break;
        }

        System.out.println("_______________________________");
        System.out.println("  Sun Mon Tue Wed Thu Fri Sat");

        int i=0;
        for(i=0;i<startDay;i++)
            System.out.print("    ");

        for(i=1;i<=numOfDaysInMonth;i++){
            if(i<10)
                System.out.print("   "+i);
            else
                System.out.print("  "+i);

            if((i + startDay) % 7 == 0)
                System.out.println();
        }

        System.out.println();
        System.out.println();

        startDay = (startDay + numOfDaysInMonth) % 7;
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片