Visual Basic程序设计教程 (李杰主编 清华大学出版社 2011.2)
习题答案 习题2参考答案
2.2 单选题
1、B 6、B
2、C 7、D
3、A 8、C
4、D 9、B
5、A 10、D
2.3 填空题
1、事件驱动 2、Cancel 3、Name
4、Private Sub Form1_Click() 5、运行程序 6、代码窗口
7、Text1.Text = \我是一个学生\ 8、LostFocus()事件
9、Label1.Caption = \请输入用户名\ Label1.ForeColor = vbGreen 10、(Text1.Text)*2
2.4 事件练习题
1、① 该事件的对象是标签Label1,单击标签触发该事件。
② 缺省Label1对象的代码是针对窗体,其余有Label1对象的代码是针对标签的。 ③ Label1.AutoSize = True语句使标签的大小自动随文字的多少而改变。
2、程序运行后,单击命令按钮,在Text1文本框显示\知音海外版\,在Text2文本框显示
\计算机世界\。
3、单击“显示文本框”命令按钮触发的事件过程
Private Sub Command1_Click() Text1.Visible = True
Text1.ForeColor = vbRed Text1.Text = \程序设计\
End Sub
单击“隐藏文本框”命令按钮触发的事件过程
Private Sub Command2_Click() Text1.Visible = False
End Sub
习题3参考答案
3.2 单选题
1 C 2 C 3 D 4 B 5 B 6 B 7 B 8 C 9 D
3.3填空题
1.REM(或 ‘) _ : 2 双引号(“”) # 3 $ % 4 UCASE()
5.(log(1+d*d)-exp(2))^(5/2) [ 还有一种理解方式:(log(1+d*d)-e*e)^(5/2) ] 6. &O113 &H4B
习题4参考答案
4.2 单选题
BDCBD
4.3 填空题
1. MsgBox(\是否删除? \删除\2. The Length of 12345=5 3. 变量或表达式 4. 1 5. loop
4.4 程序设计题
1.
Private Sub Command1_Click() If Text1.Text = \ MsgBox \密码正确!\Else
MsgBox \密码错误\错误\End If End Sub
2. < 题目中的“连续产生n*n个字母”应该改为“连续产生n个字母”> Private Sub Command1_Click() Dim n%, y%, i% n = Val(Text1.Text)
If n > 10 Or n<=0 Then
MsgBox \数据超界!\Text1.Text=”” Text1.SetFocus Else
y = Int((90 - 65 + 1) * Rnd) + 65 For i = 1 To n
Print Chr(y + i) Next i End If End Sub 3.
Private Sub Form_Click()
Dim a!, b!, c!, x1!, x21, d! a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) d = b * b - 4 * a * c If d >= 0 Then
x1 = (-b + Sqr(d)) / (2 * a) X2 = (-b - Sqr(d)) / (2 * a) Print x1; X2 Else
Print \没有实根\ End If End Sub 4.
Private Sub Form_Click() Dim i%, j%, s%, n%,q% n = InputBox(\ q = 0
For i = 1 To n s = 0
For j = 1 To i s = s + j Next j q = q + s Next i Print q End Sub 5.
Private Sub Form_Click() Dim i%, j%, n%, k% n = 0
For i = 1 To 3 For j = 1 To 3 For k = 1 To 3 Print i & j & k, n = n + 1
If n Mod 5 = 0 Then Print Next k Next j Next i Print
Print \End Sub 6.
Private Sub Form_Click() Dim i%, j%, n%, k% For i = 1 To 9 For j = 0 To 9 For k = 0 To 9
n = i * 100 + j * 10 + k
If n = i ^ 3 + j ^ 3 + k ^ 3 Then Print n, Next k Next j Next i End Sub
习题5参考答案5.2 单选题
1 A 2 B 3 C 4 C 5 D 6 D 7 A 8 C 9 5.3 填空题
1.False 2.Caption 3.0 ListCount-1 4.Print List1.List(3)
5.Combo1.ReMoveItem 3 6.LargeChange 7.Timer 8.500 9.Shape
D 10 A A 12 B
11 10.Borderstyle
5.4 程序设计题
1.
Dim op1, op2, result As Single Dim operator As Integer
Private Sub Command1_Click(Index As Integer) If Index >= 0 And Index <= 10 Then If txt1.Text = \
txt1.Text = CStr(Index + 1) Else
txt1.Text = txt1.Text + CStr(Index + 1) End If End If
If Index >= 12 And Index <= 15 Then op1 = CDbl(txt1.Text) operator = Index txt1.Text = \ End If
If Index = 11 Then op2 = CDbl(txt1.Text) Select Case operator Case 12
result = op1 + op2 Case 13
result = op1 - op2 Case 14
result = op1 * op2 Case 15
result = op1 / op2 End Select
txt1.Text = CStr(result) End If End Sub
Private Sub Command2_Click() op1 = 0 op2 = 0 result = 0 txt1.Text = \End Sub
Private Sub Command3_Click() End End Sub