wmf21 2014-07-03 07:00 采纳率: 0%
浏览 1516

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;
    }
}

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题