VB程序设计习题集(附答案)

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,读取它的全部内容并显示在文本框中。

35

根据程序写运行结果

1、运行下面的程序,单击窗体后在窗体显示的内容是___________。 Private Sub Form_Click()

Dim x As Integer, y As Integer x = 1: y = 0 Do While x < 3 y = y + x x = x + 1 Loop Print x, y End Sub

2、运行下面的程序,单击窗体后在窗体显示的第三行内容是___________。 Private Sub Form_Click()

Dim mystr As String, mystr1 As String, mystr2 As String mystr1 = \ For i = 1 To 3

mystr2 = LCase(mystr1) mystr1 = mystr1 & mystr2 mystr = mystr & mystr1 Print mystr

mystr1 = Chr(Asc(mystr1) + i) Next i End Sub

3、运行下面的程序,单击命令按钮,在文本框内显示的内容是___________。 Private Sub Command1_Click() For i = 1 To 4 x = 4

For j = 1 To 3 x = 3

For k = 1 To 2 x = x + 6 Next k, j, i

Text1.Text = Str(x) End Sub

4、运行下面的程序,单击窗体后在窗体显示的内容是___________。 Option Base 1

Private Sub Form_Click()

36

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

a(i, j) = (i - 1) * 4 + j Next j Next i

For i = 1 To 3 Print a(i, 4 - i); Next i End Sub 5、如下程序运行后,在窗体上输出的第一行内容是___________,第二行内容是___________。 Private Sub Command1_Click()

Dim p As Integer, I As Integer, n As Integer p = 2: n = 20

For I = 1 To n Step p p = p + 2 n = n - 4 I = I + 2

If p >= 10 Then Exit For Next I Print p; n Print I End Sub

6、下面程序段执行一次后,窗体上有___________行输出,其中第一行上显示的是___________,第四行上显示的是___________。 Option Explicit Option Base 1

Private Sub Command1_Click()

Dim a(5) As Integer Dim i As Integer For i = 1 To 5

If i Mod 2 = 0 Then a(i) = i + 1 Else

a(i) = 5 - i End If Print a(i) Next i End Sub

7、已知程序段内容如下,在文本框Text1中输入3,在文本框Text2中输入5,当单击命令按钮Command1后,文本框Text1中显示的是___________,文本框Text2中显示的是___________。 Option Explicit

Private Sub Command1_Click()

Dim a As Integer, b As Integer, c As Integer a = Val(Text1.Text)

37

b = Val(Text2.Text) c = a + b a = c - a b = c - a

Text1.Text = CStr(a) Text2.Text = CStr(b) End Sub

8、运行下面的程序,单击窗体后在窗体显示的内容是___________。 Private Sub Form_Click() Dim a As Integer a = 2

Call sub1(a) End Sub

Private Sub sub1(x As Integer) x = x * 2 + 1 If x < 10 Then Call sub1(x) End If

x = x * 2 + 1 Print x; End Sub

9、已知某程序内容如下,当在窗体上单击时窗体上显示的内容有___________行,第一行上显示的是___________,最后一行上显示的是___________。

Function SumOrMulti(Operate As Boolean, ParamArray var()) As Long

Dim sum As Long, multi As Long If Operate Then sum = 0

For Each x In var sum = sum + x Next x

SumOrMulti = sum Else

multi = 1

For i = LBound(var) To UBound(var) multi = multi * var(i) Next i

SumOrMulti = multi End If End Function

Private Sub Form_Click()

Print SumOrMulti(True, 1, 3, 5, 7) Print SumOrMulti(False, 2, 4) End Sub

10、已知某程序内容如下,当在窗体上单击命令按钮Command1后,窗体上显示的A的值是___________,B的值是___________,C的值是___________。 Sub SS(ByVal X, ByVal Y, ByVal Z)

X = X + 1: Y = Y + 1: Z = Z + 1 End Sub

Private Sub Command1_Click()

38

联系客服:779662525#qq.com(#替换为@)