- coco
-
1.编写产生随机整数的Function过程,调用该过程输出30个1~100之内的随机数。
Private Sub Command1_Click()
For i = 1 To 30
Print abc;
If i Mod 10 = 0 Then Print
Next
End Sub
Private Function abc() As Integer
Randomize
abc = Int(Rnd * 100) + 1
End Function
2.编写Sub过程或Function过程计算n!,并依次计算S=5!+6!+7!。
Private Sub Command1_Click()
Dim i As Integer, s As Long
For i = 5 To 7
s = s + jc(i)
Next
Print s
End Sub
Private Function jc(ByVal n As Integer) As Long
jc = 1
For i = 2 To n
jc = jc * i
Next
End Function
3.编写比较两个数大小的Function过程,求多个数中的最大数。
Private Sub Command1_Click()
Dim x, y, z, t As Integer
x = Val(InputBox("x="))
y = Val(InputBox("y="))
z = Val(InputBox("z="))
t = max(x, y)
t = max(t, z)
Print "x,y,z最大值是:"; t
End Sub
Private Function max(ByVal a As Integer, ByVal b As Integer) As Integer
If a > b Then max = a Else max = b
End Function
4.编写判断奇偶数的过程。输入整数,判断其奇偶性。
Private Sub Command1_Click()
Dim x As Integer
x = Val(InputBox("x="))
If jo(x) Then Print x; "是偶数" Else Print x; "是奇数"
End Sub
Private Function jo(ByVal a As Integer) As Boolean
If a Mod 2 = 0 Then jo = True Else jo = False
End Function
- S笔记
-
1、
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 30
n = shj(i)
Print n;
If i Mod 5 = 0 Then Print
Next i
End Sub
Private Function shj(i As Integer) As Integer
Randomize
t = Int(Rnd * 100) + 1
shj = t
End Function
2、
Private Sub Command1_Click()
Dim i As Integer, s As Integer
For i = 5 To 7
s = s + fact(i)
Next i
Print "s=" & s
End Sub
Private Function fact(n As Integer) As Integer
t = 1
For i = 1 To n
t = t * i
Next i
fact = t
End Function
3、
Private Sub Command1_Click()
Dim a() As Integer, n As Integer
n = InputBox("请输入数字个数n")
ReDim a(n)
For i = 1 To n
a(i) = InputBox("请输入a(" & i & ")")
Print a(i);
If i Mod 5 = 0 Then Print
Next i
Print
Print "最大值为:" & zd(a, n)
End Sub
Private Function zd(a() As Integer, n As Integer) As Integer
Max = a(1)
For i = 1 To n
If Max < a(i) Then Max = a(i)
Next i
zd = Max
End Function
4、
Private Sub Command1_Click()
Dim n As Integer, flg As Boolean
n = Text1.Text
Call pd(n, flg)
If flg = True Then
Print n & "是偶数"
Else
Print n & "是奇数"
End If
End Sub
Private Sub pd(n As Integer, flg As Boolean)
If n Mod 2 = 0 Then
flg = True
Else
flg = False
End If
End Sub
- okok云
-
‘试一下
"给你写在一起了,自己修改
"第一题
Private Sub Command1_Click()
MsgBox Join(fc1, ",")
End Sub
Function fc1()
Dim i, arr(1 To 30)
For i = 1 To 30
Randomize
arr(i) = Int(Rnd * 100) + 1
Next
fc1 = arr
End Function
"------------------------
"第二题
Private Sub Command2_Click()
Dim sum, i
For i = 5 To 7
sum = sum + fc2(i)
Next
MsgBox "sum=" & sum
End Sub
Function fc2(n)
Dim i
fc2 = 1
For i = 1 To n
fc2 = fc2 * i
Next
End Function
"------------------------
"第三题
Private Sub Command3_Click()
Dim arr(1 To 10) As String, i
For i = 1 To 10
Randomize
arr(i) = Int(Rnd * 100) + 1
Next
MsgBox Join(arr, ",") & vbNewLine & Join(fc3(arr), ",") & vbNewLine & "最大值:" & arr(UBound(arr))
End Sub
Function fc3(arr)
Dim i, j, t
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If Val(arr(i)) > Val(arr(j)) Then
t = arr(i): arr(i) = arr(j): arr(j) = t
End If
Next
Next
fc3 = arr
End Function
"------------------------
"第四题
Private Sub Command4_Click()
Dim s, n
s = InputBox("输入一个整数:", , "3")
If IsNumeric(s) Then
s = (Val(s))
fc4 s
MsgBox IIf(s, "偶数", "奇数")
Else
MsgBox "无效的数字!"
End If
End Sub
Sub fc4(n)
n = IIf(n Mod 2 = 0, True, False)
End Sub
- nicehost
-
Private Sub Command1_Click() "第1题
Dim i As Long
Cls
For i = 1 To 30
Print CStr(SJ(1, 100))
Next i
End Sub
Private Function SJ(ByVal LowerBound As Long, ByVal UpperBound As Long) As Long
Randomize
SJ = Int((UpperBound - LowerBound + 1) * Rnd + LowerBound)
End Function
Private Sub Command2_Click() "第2题
Print "S=5!+6!+7!=" & CStr(JC(5) + JC(6) + JC(7))
End Sub
Private Function JC(ByVal N As Long) As Long
Dim i As Long
JC = 1
For i = 1 To N
JC = JC * i
Next i
End Function
Private Sub Command3_Click() "第3题
Const MAX As Double = 1000
Const N As Long = 100
Dim i As Long, j As Long, k As Long, S As String
Dim D(1 To N) As Double, E As Double
For i = 1 To N
Randomize
D(i) = Round(Rnd * MAX, 2)
S = S & CStr(D(i)) & vbTab
If i Mod 10 = 0 Then
Print S
S = ""
End If
Next i
If N Mod 10 <> 0 Then Print S
"以上取得多个随机数
E = D(1)
For i = 2 To N
E = BJ(E, D(i))
Next i
Print "最大数为:" & CStr(E)
End Sub
Private Function BJ(ByVal A As Double, ByVal B As Double) As Double
If A > B Then BJ = A Else BJ = B
End Function
Private Sub Command4_Click() "第4题
Dim N As Long
N = Val(InputBox("请输入一个整数"))
If PD(N) Then
MsgBox "输入的整数 " & CStr(N) & " 是一个奇数"
Else
MsgBox "输入的整数 " & CStr(N) & " 是一个偶数"
End If
End Sub
Private Function PD(ByVal A As Long) As Boolean "True-奇数,False-偶数
If A Mod 2 = 1 Then PD = True Else PD = False
End Function