第1个回答 2009-05-27
'这两个问题实际可以归结为一个问题,即求某年某月有多少天,调用下面仅 1 条语句的简单函数就可以实现:
'窗体添加控件 Command1、Command2 就可以查看 GetDays 的用法
Private Function GetDays(nYear As Long, nMonth As Long) As Long
GetDays = Day(DateSerial(nYear, nMonth + 1, 0))
End Function
'问题1,通过判断 2 月份是否有 29 天确定是否闰年
Private Sub Command1_Click()
Dim nYear As Long
nYear = Val(InputBox("请输入年分,例如:", "闰年判断", "2009"))
If nYear < 1 Then Exit Sub
If GetDays(nYear, 2) = 29 Then MsgBox nYear & " 闰年" Else MsgBox nYear & " 不闰年"
End Sub
'问题2,某年某月有多少天
Private Sub Command2_Click()
Dim nStr As String, nYear As Long, nMonth As Long, S As Long
nStr = InputBox("请输入年月,例如:", "某年某月有多少天", "2009-5")
If nStr = "" Then Exit Sub
S = InStr(nStr, "-")
nYear = Left(nStr, S - 1): nMonth = Val(Mid(nStr, S + 1))
S = GetDays(nYear, nMonth)
MsgBox nStr & ":有 " & S & " 天"
End Sub
Private Sub Form_Load()
Command1.Caption = "闰年": Command2.Caption = "天数"
End Sub