我需要为接受日期的程序创建一个方法,如果有效,将添加从 01/01/xx 到当年日期的所有天数,例如 10/1/1999 将显示“1999 年第 274 天”。我在下面有以下代码,但它没有添加正确的值。不知道我做错了什么。
public static int dayNumber(int day, int month, int year){
int daysInMonth = 0;
int days = 0;
boolean leapYear = isLeapYear(year);
for(int i = 1; i <= month; i++){
switch(month){
case 1: daysInMonth += 31;
case 2: if(leapYear)
daysInMonth += 29;
else
daysInMonth += 28;
case 3: daysInMonth += 31;
case 4: daysInMonth += 30;
case 5: daysInMonth += 31;
case 6: daysInMonth += 30;
case 7: daysInMonth += 31;
case 8: daysInMonth += 31;
case 9: daysInMonth += 30;
case 10: daysInMonth += 31;
case 11: daysInMonth += 30;
case 12: daysInMonth += 31;
break;
default:
break;
}
while(month <= 12){
days += daysInMonth + day;
month++;
}
}
return days;
}