これは私の更新されたコードですが、まだ機能しません。2 月 29 日のすべての場合に日を返します。うるう年の場合は日のみを返し、うるう年でない場合は 1 を返します。
public int checkDay (int day)
{
// For months with 30 days.
if ((month == 4 || month == 6 || month == 9 || month == 11) && day <= 30)
return day;
// For months with 31 days.
if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && (day <= 31))
return day;
// For leap years.
// If February 29th...
if (month == 2 && day == 29)
{
// Check if year is a leap year.
if ((year%4 == 0 && year%100!=0) || year%400 == 0)
{
// If year is a leap year return day as 29th.
return day;
}
// If not a leap year, return day as 1st.
else return 1;
}
// If Date if February 1st through 28th return day, as it is valid.
if (month == 2 && (day >= 1 && day <= 28))
return day;
// Return day as 1st for all other cases.
return 1;
}