tzzt01

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

方法一、直接访问表

import java.util.Scanner;
import java.util.Calendar;

class DaysPerMonth {
    public static void main(String[] args) {
        
        System.out.println("输入年份:");
        Scanner scan = new Scanner(System.in);
        String input_year = scan.nextLine();
        int year = Integer.parseInt(input_year);

        System.out.println("输入月份:");
        String input_month = scan.nextLine();
        int month = Integer.parseInt(input_month);

        int days = daysPerMonth(year, month);
        System.out.println( year + " 年 " + month + " 月有 " + days + "天");
    }

    public static int daysPerMonth(int year, int month) {
        
        boolean flag = LeapYearIndex(year);
        System.out.println("是否是闰年?" + flag);

        if(flag) {
            int[] days = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            return days[month-1];
        } else {
            int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            return days[month-1];
        }

    }

    public static boolean LeapYearIndex(int year) {
        if ( ( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) {
            return true;
        } else {
            return false;
        }
    }

}

 

posted on 2017-08-23 23:37  tzzt01  阅读(183)  评论(0编辑  收藏  举报