End Sub
Public Sub GenerateData() Dim i As Integer Dim j As Integer Dim b As Boolean For i = 1 To n b = False
Do While Not b
a(i) = Int(20 * Rnd + 1) b = True
'******错误3 ****** For j = 1 To i
If a(i) = a(j) Then b = False Exit For End If Next j Loop
Form1.Print a(i) Next i End Sub
'********错误4 ********
Public Sub Swap(ByVal a As Integer, ByVal b As Integer) Dim temp As Integer temp = a a = b b = temp End Sub
七、函数过程
程序调试35
Option Explicit Public Sub rn()
'该过程通过调用函数isLeapYear判断某年是否是闰年,若是,则打印\今年是润年\ '否则,打印\今年不是闰年\。 Const year = 2023 If ----1---- Then
Form1.Print \今年是闰年\ Else
Form1.Print \今年不是闰年\ End If End Sub
Function isLeapYear(y As Integer) As Boolean
If ----2---- Or (----3---- And ----4----) Then isLeapYear = False
48
Else
isLeapYear = True End If End Function
程序调试36
Option Explicit Public Sub total()
'该过程是计算s=7+77+777+??+(n个7组成的数),并输出。 Const n = 20 Dim s As Single Dim i As Integer For i = 1 To n
s = s + ------1------- Next i
Form1.Print \End Sub
Public Function number( -----2------ ) As Single '该函数是求出n个7所组成的数。 Dim k As Integer number = 0 For k = 1 To n
--------3--------- Next k End Function
程序调试37
Option Explicit
Public Sub findstr()
'该过程通过调用matchCount函数计算子串s2在母串s1中匹配的次数 Dim s1 As String, s2 As String
s1 = \母串 s2 = \子串 Form1.Print ----1---- End Sub
Function matchCount(str1 As String, str2 As String) As Integer '本函数计算子串str2在母串str1中的匹配次数
Dim num As Integer, i As Integer, pos As Integer num = 0
For i = 1 To ----2---- '从第1个字符开始循环找 pos = ----3----
If pos > 0 Then '找到了指定的字符串 num = num + 1 '次数加1 ----4---- '继续向前找 Else
49
Exit For '没找到,退出 End If Next i
matchCount = num End Function
程序调试38
Public Function nFactor(ByVal n As Integer) As Double '该过程用于计算n!。 Dim i As Integer Dim temp As Double ----1----
For i = 1 To n
temp = temp * i Next i
nFactor = ----2---- End Function
Public Sub summary()
'该过程用于计算1!+2!+...+20!,并打印出计算结果。 Dim sum As Double Dim i As Integer Dim n As Integer n = 20
For i = 1 To n
sum = sum + ----3---- Next i
Form1.Print \End Sub
程序调试39
Option Explicit Public Sub find()
'该过程是用于从1到10000中找出这样的数,
'该数各个位的数字的阶乘相加之和等于该数,并将结果输出。 Dim k, a, n, i Dim p As Integer For k = 1 To 10000 a = LTrim(Str(k)) n = 0
For i= ----1-----
p = Val(Mid(a, i, 1)) n= -----2---- Next i
If n = k Then form1.Print k Next k End Sub
50
Function fact(x As Integer) As Long '该函数用于计算阶乘 Dim y As Long Dim i% y = 1
For i% = 1 To x y = y * i% Next i% ----3---- End Function
程序调试40
Public Sub Transfer()
'该过程用于将一个十六进制整数转换为十进制整数。 Dim Hex As String '十六进制数 Dim Dec As Double '十进制数 Dim temp As String Dim i As Integer Dim n As Integer
Hex = InputBox(\输入一个十六进制整数\ '****** 错误1 ******* n = Val(Hex) i = 0 Do
'****** 错误2 ******* temp = Mid(Hex, i, 1)
Dec = Dec + number(temp) * 16 ^ i i = i + 1 Loop While i < n
'****** 错误3 ******
Form1.Print Hex + \转换为十进制数为\End Sub
Public Function number(str As String) As Integer '该函数过程用于将一个十六进制符号转换为数值。 Select Case str Case \ number = 10 Case \ number = 11 Case \ number = 12 Case \ number = 13 Case \ number = 14 Case \
51
number = 15 Case Else
number = Val(str) End Select End Function
程序调试41
Option Explicit
Public Sub ComMulti()
'该过程是求任意两个正整数的最小公倍数
'求最小公倍数的一种方法是先求出两个数的最大公约数, '两个数的乘积除以最大公约数的商即为最小公倍数 Dim m As Integer, n As Integer '任意两个正整数 Dim i As Integer
Dim multi As Integer '最小公倍数
'输入两个正整数,要求m与n都必须大于零 Do
m = Val(InputBox(\ n = Val(InputBox(\ Loop While -----1------ multi = -------2--------
Form1.Print m; \和\的最小公倍数是:\End Sub
'Div函数过程求任意两个正整数m和n的最大公约数
Public Function Div(m As Integer, n As Integer) As Integer '该函数过程用于求任意两个正整数的最大公约数。 Dim i As Integer
'先将两个整数中的较小数假设为最大公约数,再依次往下 '寻找能同时除尽m和n的数即为最大公约数 ----------3------------ If n < m Then Div = n End If
Do While m Mod Div <> 0 Or n Mod Div <> 0 -----------4---------- Loop End Function
程序调试42
Public Sub Combination()
'该过程是用于计算在m个数据中取出n个数据的排列组合值,计算公式为Cmn=m!/(n!*(m-n)!)。 Dim m As Integer Dim n As Integer Dim Cmn As Long Do
m = Val(InputBox(\请输入一个整数m\
52