昆明的海拔高度:如何用vb 编程出所有闰年

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/30 04:10:50
在vb 中用循环语句编程出这道题:打印1-10000中所有闰年

Private Sub Command1_Click()
Dim i As Integer
Label1.Caption = "正在打印所有闰年,这可能需要点时间,请耐心等待……"
For i = 1 To 10000
If IsRunYear(i) = True Then
Text1.Text = Text1.Text + CStr(i) + vbCrLf
DoEvents '闲置循环
End If
Next

End Sub
'判断是不是闰年的函数
Function IsRunYear(CurYear As Integer) As Boolean
Dim flag As Boolean
If CurYear Mod 4 = 0 Then
If CurYear Mod 100 = 0 Then
If CurYear Mod 400 = 0 Then
flag = True
'是闰年
Else
flag = False
End If
Else
flag = True
End If
Else
flag = False
End If
IsRunYear = flag'将变量的值赋给函数返回
End Function
注:
因为你要打印1到10000所有的年,所以程序循环了10000次,这需要一定的时间,请耐心等待!

可以改进一下
判断闰年的函数
Function IsRunYear(C as integer)as boolean
if (c mod 400=0) or (c mod 4=0) and (c mod 100<>0) then IsRunYear=true else IsRunYear=false
end function