public class BookTest6_1 { public static void main(String[] args) { for (int year = 2000; year <= 2020; year++) { System.out.println(year + “年有” + numberOfDaysINYear(year) + “天”); } } public static int numberOfDaysINYear(int year) { if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) { return 366; } else { return 365; } } }
输出结果 2000年有366天 2001年有365天 2002年有365天 2003年有365天 2004年有366天 2005年有365天 2006年有365天 2007年有365天 2008年有366天 2009年有365天 2010年有365天 2011年有365天 2012年有366天 2013年有365天 2014年有365天 2015年有365天 2016年有366天 2017年有365天 2018年有365天 2019年有365天 2020年有366天