java中的case里嵌套if条件句; 输入一个年份的某一个月份,判断这个月有多少天

时间:2023-03-14 16:26:14

public class year {
    public static void main(String arg[]){
        Scanner a=new Scanner(System.in);
        System.out.print("请输入一个年份:");
       int b=a.nextInt();
        System.out.print("请输入一个月份:");
       int f=a.nextInt();
        if (b%4==0&&b%100!=0||b%400==0)//判断是否为闰年
        {
            System.out.println(b+"年是闰年");
        }
        else{
            System.out.println(b+"年是平年");

}
            switch (f)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    System.out.println(f+"月有31天");
                    break;
                case 2:
                    if (b%4==0&&b%100!=0||b%400==0) {
                        System.out.println(f + "月有28天");
                    }
                    else {
                        System.out.println(f+"月有29天");
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    System.out.println(f+"月有30天");
                    break;
                default:
                    System.out.println("输入月份不符");
                    break;
            }

}
}