方法一、直接访问表
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; } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步