第3部分参考答案及综合训练 实验1参考答案 一、 实验参考答案 第(1)题 Private Sub Form_Click() Print "新朋友,欢迎你!" End Sub Private Sub Form_DblClick() Cls End Sub 第(2)题 Private Sub Command1_Click() Form1.Width = Form1.Width * 1.5 Form1.Height = Form1.Height * 1.5 End Sub Private Sub Command2_Click() Form1.Width = Form1.Width * 0.8 Form1.Height = Form1.Height * 0.8 End Sub Private Sub Command3_Click() End End Sub 第(3)题 Private Sub Form_Load() Form1.Caption = "窗体" Form1.Height = Screen.Height / 2 Form1.Width = Screen.Width / 2 Form1.Top = (Screen.Height - Form1.Height) / 2 Form1.Left = (Screen.Width - Form1.Width) / 2 End Sub 第(4)题 Private Sub Command1_Click() Label1.FontName = "宋体" End Sub Private Sub Command2_Click() Label1.FontName = "黑体" End Sub 第(5)题 Private Sub Form_Click() Label1.FontSize = Label1.FontSize * 2 Label1.FontBold = True Label1.FontUnderline = True End Sub 第(6)题 编写Text1的Change事件过程,Text2的Locked属性为True Private Sub Text1_Change() Text2.Text = Text1.Text End Sub 第(7)题 Private Sub Command1_Click() Label1.Caption = "VB程序设计" End Sub Private Sub Command2_Click() Label1.Caption = "" End Sub 第(8)题 命令按钮的Name属性改为C1,应用Move方法。 Private Sub Form_Click() C1.Move C1.Left + 300, C1.Top + 100 End Sub 第(9)题 在Cls方法中,对象省略则默认为窗体 Private Sub C1_Click() Print: Print Tab(8); "*" Print Tab(7); "*"; Tab(9); "*" Print Tab(6); "*"; Tab(10); "*" Print Tab(5); "*"; Tab(11); "*" Print Tab(4); "*"; Tab(12); "*" Print Tab(5); "*"; Tab(11); "*" Print Tab(6); "*"; Tab(10); "*" Print Tab(7); "*"; Tab(9); "*" Print Tab(8); "*" End Sub Private Sub C2_Click() Cls End Sub 第(10)题 Private Sub Command1_Click() Text1.Visible = False Command1.Enabled = False End Sub Private Sub Command2_Click() Text1.Visible = True Text1.Text = "VB程序设计,我有点喜欢你了!" Text1.FontSize = 16 Command1.Enabled = True End Sub 第(11)题 Private Sub Command1_Click() Text1.FontName = "隶书" Text1.FontSize = 14 Text1.BorderStyle = 0 End Sub Private Sub Command2_Click() Text2.BackColor = VBred End Sub Private Sub Command3_Click() End End Sub 第(12)题 Private Sub C1_Click() Text2.Text = Text1.SelText End Sub Private Sub C2_Click() Text1.SelText = "" End Sub 二、 练习题1参考答案 1~5BBCCB 6~10CCDAD11~15BCAAC16~20CDABA 21~25CCCBC 26~30ACCAB 31~35BBACA 36~40DDDBD 41~45BDCDA 46~50CAACC 51~55CCCCB 56~60CADBA 实验2参考答案 一、 实验参考答案 第(1)题 Private Sub Form_Click() a = 5 b = 2.5 c = 7.8 y = 3.14 * a * b / (a + b * c) Print "y="; CInt(y) End Sub 第(2)题 Private Sub Command1_Click() Text3.Text = Val(Text1.Text) + Val(Text2.Text) Label1.Caption = "+" End Sub Private Sub Command2_Click() Text3.Text = Val(Text1.Text) - Val(Text2.Text) Label1.Caption = "-" End Sub Private Sub Command3_Click() Text3.Text = Val(Text1.Text) * Val(Text2.Text) Label1.Caption = "×" End Sub Private Sub Command4_Click() Text3.Text = Val(Text1.Text) / Val(Text2.Text) Label1.Caption = "÷" End Sub Private Sub Command5_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Label1.Caption = "" End Sub Private Sub Command6_Click() End End Sub 第(3)题 Private Sub Form_Click() a = MsgBox("请确认此数据是否正确", 3 + 16, "数据检查对话框") Print "你按下的按钮的值为: "; a End Sub 第(4)题 Private Sub Command1_Click() Text1.Text = "": Text2.Text = "": Text3.Text = "" Text4.Text = "": Label1.Caption = "": Label2.Caption = "" Text1.Text = InputBox("请输入第一个数: ", "输入4个数") Text2.Text = InputBox("请输入第二个数: ", "输入4个数") Text3.Text = InputBox("请输入第三个数: ", "输入4个数") Text4.Text = InputBox("请输入第四个数: ", "输入4个数") End Sub Private Sub Command2_Click() Label1.Caption = Val(Text1.Text) + Val(Text2.Text) + Val(Text3.Text) _ + Val(Text4.Text) End Sub Private Sub Command3_Click() Label2.Caption = (Val(Text1.Text) + Val(Text2.Text) + Val(Text3.Text) _ + Val(Text4.Text)) / 4 End Sub Private Sub Command4_Click() End End Sub 第(5)题 Private Sub Command1_Click() Dim t As String t = Text1.Text Text1.Text = Text2.Text Text2.Text = t End Sub 第(6)题 Private Sub Form_Click() Dim name$, sex$, age$, nationality$ Form1.FontSize = 18 name = InputBox("请输入姓名: ", "学生情况登记") sex = InputBox("请输入性别: ", "学生情况登记") age = InputBox("请输入年龄: ", "学生情况登记") nationality = InputBox("请输入籍贯: ", "学生情况登记") Print "姓名: "; name Print "性别: "; sex Print "年龄: "; age Print "籍贯: "; nationality End Sub 第(7)题 Private Sub C1_Click() Text1.Text = LCase(Text1.Text) Text2.Text = UCase(Text1.Text) End Sub 第(8)题 Private Sub Command1_Click() Dim num%, qw%, bw%, sw%, gw% num = Text1.Text qw = num \ 1000 bw = (num - qw * 1000) \ 100 sw = (num - qw * 1000 - bw * 100) \ 10 gw = num Mod 10 Text2.Text = qw:Text3.Text = bw Text4.Text = sw:Text5.Text = gw Text6.Text = gw * 1000 + sw * 100 + bw * 10 + qw End Sub 二、 练习题2参考答案 选择题 1~5CADDC6~10DADDA11~15BCCDB 16~20ADBCA21~25CABBD 填空题 1. Int(Rnd*90+10) 2. 变体类型或Variant 3. 0.01 4. 1 5. 长整型,货币型,单精度 6. Shanghai 7. 双引号,#号 8. 字符型 9. Beijing 实验3参考答案 一、 实验参考答案 第(1)题 Private Sub Command1_Click() Dim a!, b!, c!, max! a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) max = a'设最大数是a If max b Then max = b If max c Then max = c Label2.Caption = max End Sub 第(2)题 Private Sub Command1_Click() Dim a!, b!, c!, t!, s! a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) If a + b c And a + c b And b + c a Then t = (a + b + c) / 2 s = Sqr(t * (t - a) * (t - b) * (t - c)) Label2.Caption = s Else MsgBox "输入的3个数不能构成三角形,请重新输入!", 17, "警告" End If End Sub Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = "" Label2.Caption = "" Text1.SetFocus End Sub Private Sub Command3_Click() End End Sub 第(3)题 Private Sub Form_Click() score = Val(InputBox("请输入成绩: ", "成绩转换")) If score = 90 Then Print "A" ElseIf score = 80 Then Print "B" ElseIf score = 70 Then Print "C" ElseIf score = 60 Then Print "D" Else Print "E" End If End Sub 第(4)题 Private Sub Command1_Click() x! = Val(Text1.Text) Select Case x Case Is 1 y = x s = "y=x=" & Str(y) Case Is 10 y = 2 * x - 1 s = "y=2*x-1=" & Str(y) Case Else y = 3 * x - 11 s = "y=3*x-11=" & Str(y) End Select Label2.Caption = s End Sub Private Sub Command2_Click() End End Sub 第(5)题 Private Sub C1_Click() Dim i As Integer i = Val(L4.Caption) If T1.Text = "vb" Then If T2.Text = "123456" Then MsgBox "密码正确!", 48, "正确" Else i = i - 1 If i = 0 Then MsgBox "第3次密码错误,系统将锁死!", 16, "警告" L4.Caption = 0 T1.Enabled = False: T2.Enabled = False C1.Enabled = False Else MsgBox "第" & 3 - i & "次密码错误!", 16, "警告" L4.Caption = i End If End If Else MsgBox "账号错误!", 16, "警告" End If End Sub Private Sub C2_Click() End End Sub Private Sub Form_Load() T1.Text = "": T2.Text = "" T2.MaxLength = 6: T2.PasswordChar = "*" End Sub 第(6)题 Private Sub Command1_Click() Dim x!, y! If IsNumeric(Text1.Text) Then x = Val(Text1.Text) - 1600 If x 5000 Then y = 0.2 ElseIf x 2000 Then y = 0.15 ElseIf x 500 Then y = 0.1 ElseIf x 0 Then y = 0.05 Else y = 0 End If Text2.Text = y Text3.Text = x * (1 - y) + 1600 Text4.Text = x * y Else a = MsgBox("工资应该是数值!", 21, "警告") If a = 2 Then End Else Text1.Text = "": Text2.Text = "": Text3.Text = "": Text4.Text = "" Text1.SetFocus End If End If End Sub 第(7)题 Private Sub Command1_Click() Dim a#, b#, c#, d#, t# If IsNumeric(Text1.Text) And IsNumeric(Text2.Text) _ And IsNumeric(Text3.Text) And IsNumeric(Text4.Text) Then a = Val(Text1.Text): b = Val(Text2.Text) c = Val(Text3.Text): d = Val(Text4.Text) If a b Then t = a: a = b: b = t If a c Then t = a: a = c: c = t If a d Then t = a: a = d: d = t If b c Then t = b: b = c: c = t If b d Then t = b: b = d: d = t If c d Then t = c: c = d: d = t Text1.Text = a: Text2.Text = b Text3.Text = c: Text4.Text = d Else MsgBox "请输入数字" Text1.Text = "": Text2.Text = "" Text3.Text = "": Text4.Text = "" End If End Sub 第(8)题 Private Sub Form_Click() Dim msg, userinput msg = "请输入一个字母或数字(0到9): " userinput = InputBox(msg) If Not IsNumeric(userinput) Then'不是数字 If Len(userinput) 0 Then Select Case Asc(userinput)'返回字符的ASCII码 Case 65 To 90 msg = "你输入的是一个大写字母" msg = msg & userinput & "." Case 97 To 122 msg = "你输入的是一个小写字母" msg = msg & userinput & "." Case Else msg = "你输入的不是一个字母或数字." End Select End If Else Select Case userinput Case 1, 3, 5, 7, 9'奇数 msg = userinput & "是一个奇数" Case 0, 2, 4, 6, 8'偶数 msg = userinput & "是一个偶数" Case Else'出界 msg = "你输入的数字越界" End Select End If MsgBox msg End Sub 第(9)题 Private Sub Command1_Click() Cls s1 = "我选的课程有:" If Ch1.Value = 1 Then s1 = s1 + " SQL Server数据库" End If If Ch2.Value = 1 Then s1 = s1 + " My SQL数据库" End If If Ch3.Value = 1 Then s1 = s1 + " 计算机网络" End If Print s1 End Sub