根据输入的日期计算该日期在本年的天数
package day_09; /* 根据输入的日期计算该日期在本年的天数 */ import java.util.Scanner; public class Test { public static void main(String[] args) { //输入日期 Scanner input = new Scanner(System.in); System.out.print("请输入年月日:"); int year = input.nextInt(); int month = input.nextInt(); int day = input.nextInt(); //计算对应的天数 int sumday = getSumDay(year, month, day); //输出结果 System.out.println(year + "年" + month + "月" + day + "日" + "是" + year + "年的第" + sumday + "天"); } private static int getSumDay(int year, int month, int day) { int days = 0; for (int i = 1; i <month; i++) { days += getMonthDay(year,i); } return days+day; } private static int getMonthDay(int year,int month) { int day=0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 11: day=31; break; case 2: //如果是闰年:29天,否则是:28天 day=getDay(year); break; case 4: case 6: case 9: case 12: day=30; break; } return day; } private static int getDay(int year) { if(year%4==0 || (year%100==0 && year%400==0)) return 29; else return 28; } }