
用VB编写程序,运行后自动显示某年某月有多少天,这个程序要考虑到特殊月份2月和是否是闰年。
闰年的2月份有29天,否则则是28天。

判断闰年的代码
Private Function leap_year(year As Integer) As Boolean ’判断闰年
leap_year = (year Mod 4 = 0 And year Mod 100 <> 0 Or year Mod 400 = 0)
End Function
用组合框来显示年份和月份,直接用循环语句添加
Private Sub Form_Load() ’组合框1添加年份
For i = 1960 To 2060
Combo1.AddItem i
Next i
For j = 1 To 12 ’组合框2添加月份
Combo2.AddItem j
Next j
End Sub
每个月有多少天 判断代码
Option Base 1
Private Sub Command1_Click()
Cls
Dim days%, month%, year%, day1_1%, n%
Static month_days As Variant
month_days = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
days = 0
month = Combo2.Text
year = Combo1.Text
month_days(2) = IIf(leap_year(year), 29, 28)
For K = 1 To month - 1
days = days + month_days(K)
Next K
Print Combo1.Text & "年" & Combo2.Text & "月共" & month_days(K) & "天"
End Sub
运行界面如下图
