VB编程代码
1、输出较大数:
Private Sub Command1_Click()
Dim a As Double a = Val(Text1.Text) If Val(Text1.Text) < Val(Text2.Text) Then a = Val(Text2.Text)
Label3.Caption = "较大的数为:" + Str(a)
End Sub
2、商品打折:
Private Sub Command1_Click()
Dim x As Single Dim y As Single x = Val(Text1.Text) If x < 1000 Then y = x
Else
If x < 2000 Then y = 0.95 * x
Else
If x < 3000 Then y = 0.9 * x
Else
If x < 5000 Then y = 0.85 * x
Else
y = 0.8 * x
End If
End If
End If
End If
Label2.Caption = "折扣后价格为:" + Str(y)
End Sub
3、划分成绩等级:
Private Sub Command1_Click()
Dim x As Single Dim y As String x = Val(Text1.Text) If x < 80 Then
If x < 70 Then
If x < 60 Then
y = "<不及格>"
Else
y = "<及格>"
End If
Else
y = "<中等>"
End If
ElseIf x < 90 Then y = "<良好>"
Else
y = "<优秀>"
End If
Label2.Caption = "该成绩等级为:" + y End Sub
4、求N~:
Private Sub Command1_Click()
Dim s As Single
Dim i, n As Integer s = 1
n = Val(Text1.Text) For i = 1 To n
s = s * i
Next
Label2.Caption = Str(n) + "的阶乘为:" + Str(s)
End Sub
5、随机数组:
Private Sub Command1_Click()
Dim k, a, b, cArray(100) As Integer
For k = 1 To 100
cArray(k) = Int(Rnd() * 100)
Print cArray(k);
If k Mod 10 = o Then Print
End If
Next
b = Val(Text1.Text) a = o
For k = 1 To 100
If b = cArray(k) Then a = k
Exit For
End If
Next
If a = 0 Then
Label2.Caption = "对不起,数组中没有该数~"
Else
Label2.Caption = "您找的是数组中的第" + Str(a) + "个数。" End If
End Sub
6、最大公约数:
Private Sub Command1_Click() Dim a As Integer, b As Integer, c As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
c = a Mod b
Do While c > 0
a = b
y = c
c = a Mod y
Loop
Label3.Caption = "最大公约数=" & y
End Sub
7、判断质数:
Private Sub Command1_Click() Dim i As Integer, j As Integer i = Val(Text1.Text)
For j = 2 To Int(Sqr(i)) If i Mod j = 0 Then
Print i & "不是质数"
Exit For
End If
Next
If j > Int(Sqr(i)) Then Print i & "是质数"
End If
End Sub
8、100以内所有质数:
Private Sub Command1_Click() Dim i As Integer, j As Integer For i = 2 To 100
For j = 2 To Int(Sqr(i)) If i Mod j = 0 Then
Exit For
End If
Next
If j > Int(Sqr(i)) Then
Print i
End If
Next
End Sub
9、前100个质数:
Private Sub Command1_Click() Dim i As Integer, j As Integer, n As Integer
i = 2
Do
For j = 2 To Int(Sqr(i)) If i Mod j = 0 Then Exit For
End If
Next
If j > Int(Sqr(i)) Then Print i,
n = n + 1
If n Mod 5 = 0 Then Print End If
i = i + 1
Loop Until n >= 100 End Sub
10、九九乘法表:
Private Sub Command1_Click() Dim i As Integer, k As Integer, j As Integer
Print "* "
For i = 1 To 9
Print Tab(i * 6); i; Next
Print
For k = 1 To 9
Print k;
For i = 1 To k
j = i * k
Print Tab(i * 6); j; Next i
Print
Next k
End Sub
11、百钱百鸡求解:
Private Sub Command1_Click()
Dim x As Integer, y As Integer, z As Integer, m, n As Integer
For x = 0 To 20
For y = 0 To 34
For z = 0 To 300
m = x + y + z
n = 5 * x + 3 * y + z / 3 If m = 100 And n = 100 Then
Print "鸡翁:", x, "鸡母:", y, "鸡雏:", z End If
Next z
Next y
Next x
End Sub
12、求1~+2~+…+N!:
Private Sub Command1_Click()
Dim s As Single
Dim i, n As Integer s = 0
n = Val(Text1.Text) For i = 1 To n
s = s + my_function(i) Next
Label2.Caption = "阶乘和S的值为:" & s End Sub
Public Function my_function(ByVal n As Integer) As Single
Dim m As Single
m = 1
For k = 1 To n
m = m * k
Next k
my_function = m
End Function