老师布置的作业中,有涉及写一个函数是否为闰年。Insus.NET分别写了C#和VB.NET,以作备忘与参考。
C#:
public bool IsLeapYear(int year)
{
if ((year < 1) || (year > 9999))
{
throw new ArgumentOutOfRangeException("year", "年份必须是从1至9999之间数字.");
}
if ((year % 4) != 0)
{
return false;
}
if ((year % 100) == 0)
{
return ((year % 400) == 0);
}
return true;
}
VB.NET:
Public Function IsLeapYear(year As Integer) As Boolean
If (year < 1) OrElse (year > 9999) Then
Throw New ArgumentOutOfRangeException("year", "年份必须是从1至9999之间数字.")
End If
If (year Mod 4) <> 0 Then
Return False
End If
If (year Mod 100) = 0 Then
Return ((year Mod 400) = 0)
End If
Return True
End Function