VB程序习题集(附答案) 下载本文

. . . .. .

Text2 = Text2 & Str(Sort(I)) End Sub

8、如下程序的功能是找出200以内所有的素数,并按每行10个的形式输出。完善程序代码。

Option Base 1 Option Explicit

Private Sub Form_Click()

Dim Prime() As Integer, I As Integer

Dim K As Integer, M As Integer, J As Integer M = 1

Redim Prime(M) Prime(M) = 2

For I = 3 To 199 Step 2

For K = 2 To ___________

If ___________Then Exit For Next K

If K > Sqr(I) Then

M = M + 1

Preserve Prime(M) Prime(M) = I End If Next I

For J = 1 To Ubound(Prime) Print Prime(J);

If J Mod ___________ = 0 Then Print Next J End Sub

9、下面的程序功能是:生成100个 -10到10之间的随机整数并显示到文本框中,统计这100个数中 -10 、-5 、 5 、10的个数并显示在图片框中。请完善程序。 Dim A(1 To 100) As Integer Private Sub Command1_Click()

Dim num1 As Integer, num2 As Integer, num3 As Integer, num4 As Integer num1 = 0: num2 = 0: num3 = 0: num4 = 0 For I = 1 To 100

Select Case ___________ Case -10

num1 = num1 + 1 Case -5

num2 = num2 + 1 Case 5

num3 = num3 + 1 Case 10

num4 = num4 + 1 End Select Next I

Picture1.Print \的个数=\ab(15); \的个数=\

c. .. .. .

. . . .. .

Picture1.Print \的个数=\ab(15); \的个数=\End Sub

Private Sub Form_Load()

Text1.Text = \For I = 1 To 100

___________= Int(Rnd() * 21 - 10)

Text1.Text = ___________& Str(A(I)) & \ \Next I End Sub

10、下面的过程用比较交换法实现数据从大到小的排序,请完善代码。 Sub SortArray(a() As Integer) Dim i As Integer, j As Integer Dim n As Integer, t As Integer ___________= UBound(a) For i = 1 To n - 1

For j =___________ To n If a(i) < a(j) Then t =___________ a(i) = a(j)

___________= t End If Next j Next i End Sub

11、下列程序代码读入N个数,用比较交换法对这N个数按从大到小的顺序排序,并在文本框Text1中输出排序结果。 Private Sub Command1_Click()

Dim a() As Integer

Dim i As Integer, j As Integer Dim t As Integer, n As Integer

n = Val(InputBox(\请输入正整数N:\ ReDim ___________ For i = 1 To n

___________= Val(InputBox(\请输入第\个数\ Next i

For i = 1 To n - 1

For j = ___________To n If a(i) < a(j) Then t = a(i)

a(i) =___________ a(j) = ___________ End If Next j Next i

For i = 1 To n

Text1.Text = Text1.Text & Str(a(i)) & \ Next i End Sub

12、以下程序代码实现单击命令按钮Command1时生成20个[0,100]之间的随机整数,存于数组中,打印数组中大于50的数,并求这些数的和。 Private Sub Command1_Click()

Dim a(1 To 20) As Integer

c. .. .. .

. . . .. .

Dim i As Integer, sum As Integer For i = 1 To 20

a(i) = ___________ Picture1.Print a(i); Next i

For i = 1 To 20

If ___________Then Picture2.Print a(i); sum = ___________ End If Next i

Picture2.Print

Picture2.Print \End Sub

13、下列程序的功能是:在窗体上生成包含有[1,10]之间随机整数的6行6列的矩阵,并对该矩阵进行转置。要求完善程序。 Option Base 1

Private Sub form_Click()

Dim a(6, 6) As Integer, b(6, 6) As Integer For i = 1 To 6 For j = 1 To 6

a(i, j) = ___________ Print a(i, j); Next j

___________ Next i Print

For i = 1 To 6 For j = 1 To 6

b(i, j) = ___________ Print b(i, j); Next j Print Next i End Sub

14、下面程序的功能是:在窗体上输出一个N行、N列、主对角线和次对角线元素为1、其余元素均为0的矩阵。N由用户指定。请完善程序。 Option Base 1 Dim A() As Integer

Private Sub Form_Click()

Dim N As Integer, I As Integer, J As Integer

N = Val(InputBox(\请输入N值\生成矩阵\ReDim For I = 1 To N For J = 1 To N A(I, J) = 0

If ___________Then A(I, J) = 1

c. .. .. .

. . . .. .

End If

If I + J = N + 1 Then A(I, J) = 1 End If Next J, I

For I = 1 To N For J = 1 To N

Print ___________; Next J Print Next I End Sub

15、下列程序的功能是:计算裴波那契数列第n项的值(n由用户输入)。要求完善程序。 Private Sub form_Click() Dim n As Integer

n = InputBox(\输入整数n\ Print f___________ End Sub

Private Function f(ByVal a As Integer) As Integer If ___________Then f = 0

ElseIf a = 2 Then ___________ Else

f = ___________ End If End Function

16、下面程序的功能是:在窗体的文本框Text1中输入一个正整数,当单击命令按钮后,在窗体的文本框Text2中输出这个正整数的阶乘。请完善程序。 Option Explicit

Function Fact(N As Integer) As Long

Dim I As Integer, F As Long F = ___________

For I = 1 To F = F * I Next I

___________ End Function

Private Sub Command1_Click()

Dim X As Integer X = Val(Text1.Text)

Text2.Text = Str___________ End Sub

17、下面是求两个正整数m和n的最大公约数的函数,请完善代码。 Function gcd(m As Integer, n As Integer) As Integer

Dim r As Integer r = m Mod n

Do While r___________0 ___________= n

c. .. .. .

. . . .. .

___________= r r = m Mod n Loop gcd = n End Function

18、在窗体上画一个命令按钮和一个文本框,其名称分别为Command1和Text1,然后编写如下事件过程:

Private Sub Command1_Click()

Dim St As String Text1.Text = \

Open \Do While ___________ Input #1, St

Text1.Text = Text1.Text & St Loop End Sub

程序的功能是,打开D盘根目录下的文本文件myfile.txt,读取它的全部内容并显示在文本框中。

c. .. .. .