Option Explicit
Private a As Integer, b As Integer, c As Integer
Private Sub Form_Load()
'3 判定2000-2500年中每一年是否是闰年,然后将是闰年的年份打印在立即窗口中。
Dim i As Integer
For i = 2000 To 2500
If (i Mod 4 = 0 And i Mod 100 <> 0) Or i Mod 400 = 0 Then
Debug.Print i,
End If
Next
End Sub
Private Sub Form_Click()
On Error Resume Next
'1 在键盘上用inputbox函数输入3个数,然后用MSGBOX函数显示出最大值,
' 最小值及两个数之和。Max() min() sum =(max()+min())
a = InputBox("a")
b = InputBox("b")
c = InputBox("c")
MsgBox "最大值是:" & max & vbCrLf & "最小值是:" & min & vbCrLf & "两数之和:" & (min + max)
'2.在键盘上用inputbox函数输入3个数,然后用MSGBOX函数由小到大输出。
' 例如输入2,3,1,则输出1<2<3
a = InputBox("a")
b = InputBox("b")
c = InputBox("c")
MsgBox "由小到大输出:" & min2max
'2 编写函数过程,参数为1个正整数,如果该数为偶数则返回2,如果为奇数则返回1,如果是0则返回0。。
a = InputBox("a")
MsgBox a & "是" & IIf(isEven(a), "偶数", "奇数")
'4 编写子过程,参数为一个大于或等于3的正整数,判定其是否是素数,并将结果显示在立即窗口中。
a = InputBox("a")
Debug.Print a & IIf(isPrime(a), "是", "不是") & "素数"
'5 在键盘上用inputbox函数输入1个成绩,判断其的等级。然后用MSGBOX函数输出。小于60分等级为C,60-90等级为B,90分以上为A。
a = InputBox("a")
MsgBox a & "的等级是" & level(a)
'6 输入两个数,计算其最大公约数和最小公倍数,并输出(使用多种方法)
a = InputBox("a")
b = InputBox("b")
MsgBox "最大公约数是" & gcd(a, b) & vbCrLf & "最小公倍数是" & lcm(a, b)
End Sub
Private Sub Command1_Click()
'7 在一窗体中建立一个文本框a,标签b,命令按钮c,在a中输入英文字符串,
' 点击命令按钮,则在标签中显示转换的结果。转换方法为:大小写视为小写lcase(),
' 将a转换为b,b转换为c,………………z转换为a。
Dim s As String, c As String * 1
Dim i As Integer
s = LCase(text1.Text)
label1.Caption = ""
For i = 1 To Len(s)
c = Chr(Asc(mid(s, i, 1)) + 1)
If c > "z" Then
c = "a"
End If
label1.Caption = label1.Caption + c
Next
End Sub
Function isEven(num) As Boolean
isEven = (num Mod 2 = 0)
End Function
Function level(score As Integer) As String
Dim l As String
Select Case score
Case Is < 60
l = "C"
Case 60 To 90
l = "B"
Case Is > 90
l = "A"
End Select
level = l
End Function
Function gcd(ByVal a As Integer, ByVal b As Integer)
Dim temp As Integer
If b = a Then
gcd = b
Exit Function
End If
temp = 0
While ((b Mod a) <> 0)
temp = b
b = a
a = temp Mod a
Wend
gcd = a
End Function
Function lcm(ByVal a As Integer, ByVal b As Integer) As Integer
lcm = IIf(a < b, b, a) / gcd(a, b) * IIf(a > b, b, a)
End Function
Function max() As Integer
Dim k
k = IIf(a > b, a, b)
max = IIf(k > c, k, c)
End Function
Function min() As Integer
Dim k
k = IIf(a < b, a, b)
min = IIf(k > c, c, k)
End Function
Function isPrime(ByVal num As Long) As Boolean
Dim i As Integer
Dim flag As Boolean
flag = True
For i = 2 To Sqr(num)
If num Mod i = 0 Then
flag = False
Exit For
End If
Next
isPrime = flag
End Function
Function middle() As Integer
middle = a + b + c - max - min
End Function
Function min2max() As String
min2max = min & "<" & middle & "<" & max
End Function
温馨提示:内容为网友见解,仅供参考