书本习题参考答案 Visual Basic 程序设计基础 浙江科技出版社 陈庆章 主编.doc
Visual Basic程序设计基础教材习题参考答案第1章 Visual Basic 6.0程序设计概述一、判断题1 2 3× 4 5× 6 7× 8 9 10× 11 12 13× 14 15× 16 17 18 19 20二、选择题1. C 2. B 3D 4C 5B 6A 7D 8A 9B 10A 11C 12D三、填空题1对象、事件 2属性、方法、事件 3控件、屏幕(Screen)4对象 5Left、Top 6属性、<对象名>.<属性名>=<表达式>7网格 8Form19Load或Initialize或Resize或Activate(自动执行的先后顺序也按此排列)10Activate、Deactivate四、程序设计题程序1界面设计参看图10-1、10-2以及表10-1。表10-1 实例2各控件的主要属性设置控件属性(属性值)属性(属性值)标签控件1Name(Label1)Caption("我的第一个VB程序")标签控件2Name(Label2)Caption("请输入你的姓名")FontUnderLine(True)FontItalic(True)命令按钮1Name(Command1)Caption("确定(&O)")命令按钮2Name(Command2)Caption("结束(&X)")过程设计如下:Private sub Form_Click() Command2.Enabled = FalseEen SubPrivate Sub Command1_Click() Label2.Visible = False Label1.Caption = Text1.Text + ": 欢迎使用" + Label1.Caption Text1.Visible = False Command1.Enabled = False: Command2.Enabled = TrueEnd SubPrivate Sub Command2_Click() EndEnd Sub程序2界面设计略,过程设计如下:Private Sub Command1_Click() '字体变大 Form1.FontSize = Form1.FontSize + 3 Form1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command2_Click() '字体变小 Form1.FontSize = Form1.FontSize - 3 Form1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command3_Click() '字体加粗 Form1.FontBold = True Form1.Cls: Print "欢迎使用VB"End SubPrivate Sub Command4_Click() '字体不加粗 Form1.FontBold = False Form1.Cls: Print "欢迎使用VB"End Sub运行时,不得连续、过多次单击“字体变小”按钮,因为当FontSize属性值小于0会产生适时错误。程序3界面设计略,过程设计如下(窗体Height值的计量包括标题、边框,而ScaleHeight值仅与窗体有效绘图区域的高度有关):Private Sub Form_Load() Text1.Text = "Visual Basic 程序设计"End SubPrivate Sub Form_Resize() Text1.Top = 0: Text1.Left = 0 Text1.Width = Form1.ScaleWidth/2 '用 Form1.ScaleWidth效果优于用Form1. Width Text1.Height = Form1.ScaleHeight/2 '用 Form1.ScaleHeight优于用Form1. Height Command1.Top = Form1.ScaleHeight - Command1.Height Command1.Left = Form1.ScaleWidth - Command1.WidthEnd Sub第2章 程序设计基础一、判断题1 2× 3× 4× 5 6× 7 8 9 10×二、选择题1B 2D 3A 4C 5C 6B 7A 8D 9. C 10A三、填空题1String 24 3"aaa" 4145 56 63 718 8False9(x Mod 10) * 10 + x 10 1010 + Int( Rnd * 90 ) 11a * b Mod c12Log(x)+Sin(3.141593/6) 13Const PI=3.1415926 14日期 15Int(x)+1四、程序设计题程序1界面设计略,过程设计如下:方法一、用文本框控件输入圆的半径Private Sub Command1_Click() Dim x As Single x = Text1.Text Label1.Caption = x * x * 3.141593End Sub方法二、用InputBox函数输入圆的半径Private Sub Command1_Click() Dim x As Single '若要求计算结果具有15位有效位数,声明x为Double类型 x = InputBox("r=","输入圆的半径") Label1.Caption = x * x * 3.141593End Sub第3章 结构化程序设计与数组一、判断题1 2× 3 4× 5 6× 7× 8 9× 10二、选择题1A 2D 3C 4C 5D 6B三、填空题1If x>y Then t=y: y=x: x=t 2IS3For i = 0 to 9: B(i) = InputBox("B(" & i & ")="): Next i4Dim 5String 68四、程序阅读题(写出下列程序的运行结果)程序1. 0 5 程序2. 20 程序3. 2 4 7 11 16程序4. 1 4 9 16 25程序5. 程序6. 五、程序填空题1(1)q * i (2)s + p * i / q (3)Next i2(1)r = 0 (2)m Mod n3(1)i + j - 1 (2)7 - i (3)Print4(1)10 + Int(Rnd * 90) (2)i + 1 To 30 (3)a(i)=a(j) (4)a(i) Mod 2 = 1六、程序设计题程序1界面设计略,过程设计如下:Private Sub Form_Click() Dim x As Integer, y As Integer, z As Integer Dim max As Integer, midd As Integer, min As Integer x = InputBox("请输入x的值:") y = InputBox("请输入y的值:") z = InputBox("请输入z的值:") If x > y Then max = x Else max = y If z > max Then max = z If x < y Then min = x Else min = y If z < min Then min = z midd = x + y + z - max - min Print max; midd; minEnd Sub程序2界面设计略,过程设计如下:Private Sub Form_Click() Dim x As Single, y As Single x = InputBox("请输入x的值:") If x > 3 Then y = x + 3 ElseIf x >= 1 Then y = x * x ElseIf x > 0 Then y = Sqr(x) Else y = 0 End If Print yEnd Sub程序3界面设计略,过程设计如下:Private Sub Form_Click() Dim i As Integer, j As Integer For i = 1 To 9 For j = 1 To 9 Print Tab(j - 1) * 4); i * j; Next j Print Next iEnd Sub程序4界面设计略,过程设计如下:Private Sub Form_Click() Dim x As Single, s As Single, t As Single Dim i As Integer, n As Integer x = InputBox("请输入x的值:") n = InputBox("请输入n的值:") t = x / 2: s = t For i = 3 To n t = t * x / i s = s + t Next i Print sEnd Sub程序5界面设计略,过程设计如下:Private Sub Form_Click() Dim e As Single, t As Single, i As Integer e = 2: b = 1: i = 1 Do i = i + 1 a = b b = b / i e = e + b Loop While Abs(b - a) >= 0.0001 Print "e=" eEnd Sub程序6界面设计略,过程设计如下:Option Base 1Private Sub Form_Click() Dim n As Integer, a(9, 9) As Integer, i As Integer, j As Integer n = InputBox("请输入n,0<n<10") For i = 1 To n: a(i, 1) = 1: a(i, i) = 1: Next i For i = 3 To n For j = 2 To i - 1 a(i, j) = a(i - 1, j - 1) + a(i - 1, j) Next j, i For i = 1 To n Print Tab(30 - 2 * i); For j = 1 To i Print Space(4 - Len(Trim(Str(a(i, j); Trim(Str(a(i, j); Next j Print Next iEnd Sub第4章 函数与过程一、判断题1× 2 3× 4× 5 6× 7× 8二、填空题1按地址传送 2b() As Long 36 4按值传递 5按地址传递6Public x As Single 7Static x As Integer 8Form2.y三、程序阅读题(写出下列程序的运行结果)程序1. s = 2 程序2. 1 程序3. 1 1 2 1 1 2 1 2 2 2 2 3 s = 5 1 1 s = 9 1 2 1 1 3 3 1 1 4 6 4 1四、程序填空题1(1)ByVal (2)k Mod i (3)k = k i (4)Call pp(i)2(1)a() As Double (2)n-1 (3)a(j) < a(k)3(1)a() As Double, n As Integer (2)t = t * x (3)f = s五、程序设计题程序1界面设计略,过程设计如下:Private Function fsum(x() As Double, n As Integer) As Double Dim i As Integer For i = 1 To n fsum = fsum + x(i) Next i fsum = fsum / nEnd Function程序2界面设计略,过程设计如下:Private Sub ff(a() As Single, n As Integer) Dim i As Integer, x As Single For i = 1 To n 2 x = a(n - i + 1): a(n - i + 1) = a(i): a(i) = x Next iEnd Sub程序3界面设计略,过程设计如下:Private Sub find(x() As Single, m As byte, n As Byte, _xmax As Single,ki As Byte,kj As Byte) xmax = x(1,1): ki = 1: kj = 1 For i = 1 To m For j = 1 To n If Abs(x(i, j) > Abs(xmax) Then xmax = x(i, j): ki = i: kj = j Next j, i End Sub第5章 常用控件一、判断题1× 2× 3× 4 5× 6 7× 8 910 11× 12× 13 14 15× 16× 17× 1819 20× 21 22× 23 24× 25二、选择题1C 2D 3B 4B 5B 6C 7B 8B 9B 10A11D 12C 13B 14B 15A 16D 17B 18B 19B 20A三、填空题1上、下、左、左 2缇、无关 3&、<Alt>+<Y> 4Enabled 5MaxLength6Text1.SetFocus 7MultiLine 8Visible 9Alignment 10AutoSize11ForeColor 120或1 13True 141 15AddItem161 17List1.Clear 18文本框、列表框19下拉式组合框、简单组合框、下拉式列表框 20Scroll 21Value22Change 23定时器不起作用 24Timer 2565535四、程序阅读题程序1. 116 程序2. 8程序3. 23451 程序4. 小,你好! 程序5. 您好34512 小李,你好! 欢迎使用Visual Basic!程序6. 李子 程序7.y = 6 程序8.n = 1苹果 y = 14 n = 3 橘子 n = 5 葡萄 柚子 香蕉五、程序填空题1(1)a(i) = Mid(str1, i, 1) 或 a(i) = Mid(Text1.Text, i, 1) (2)p = j (3)a(i) = a(p) (4)Command2.Enabled = True2(1)1 To 2*i1 (2)Command2.Enabled = True (3)Command2.Enabled = False3(1)Text2.Enabled = False (2)p = 2 (3)Print i (4)Val(Text1.Text) < 2 或 Val(Text1.Text) <= 1 (5)KeyAscii = 134(1)List1.ListCount (2)List1.RemoveItem I (3)i = i + 15(1)Timer1.Enabled = True (2)x 3600 (3)(x Mod 3600) 60 或 (x - 3600 * h) 60 (4)x = x + 1 6(1)Label1.Left (2)Label1.Left = -Label1.Width六、程序设计题程序1. 界面设计略,过程设计如下:Private Sub Command1_Click() If Command1.Caption = "显示" Then Print "欢迎使用Visual Basic!": Command1.Caption = "清除" Else Form1.Cls: Command1.Caption = "显示" End IfEnd SubPrivate Sub Command2_Click() EndEnd SubPrivate Sub Form_Load()Form1.Cls: Command1.Caption = "显示": Command2.Caption = "退出"End Sub程序2界面设计略,过程设计如下:Private Sub Form_Load() Text1.MaxLength = 1: Text1.Text = ""End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) Label1.Caption = CStr(KeyAscii)End Sub程序3界面设计略,过程设计如下:Const pwd As String = "123456" '预设密码为123456Private Sub Command1_Click() Cls: Print "欢迎进入!"End SubPrivate Sub Form_Load() Command1.Caption = "进入": Command1.Enabled = Falsew Text1.Text = "": Text1.PasswordChar = "*"End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then If Text1.Text = pwd Then m = MsgBox("口令正确,欢迎进入!"): Command1.Enabled = True Else m = MsgBox("口令错误,请重新输入!"): Text1.Text = "" End If End IfEnd Sub程序4界面设计略,过程设计如下:Dim num As Integer, m As Integer, rec As IntegerPrivate Sub Form_Load() Command1.Caption = "出题": Command1.Enabled = True Text1.Text = "": Text1.Enabled = False: rec = 0End SubPrivate Sub Command1_Click() Randomize: num = Int(Rnd * 100) + 1: Text1.Enabled = True Label1.Caption = "": Command1.Enabled = FalseEnd SubPrivate Sub Command2_Click() EndEnd SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then m = Val(Text1.Text): rec = rec + 1: Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text): Text1.SetFocus If rec > 10 Then Label1.Caption = "差!重新开始": Call Form_Load Else If m > num Then Label1.Caption = Str(m) & "大了!" ElseIf m < num Then Label1.Caption = Str(m) & "小了!" Else If rec < 5 Then Label1.Caption = "好!猜对了。" Else Label1.Caption = "还行!猜对了。" End If Call Form_Load End If End If End IfEnd Sub程序5界面设计如图10-3所示,过程设计如下:Private Sub Form_Load() Command1(0).Enabled = False: Command1(1).Enabled = False Command1(2).Enabled = False: Label1.Caption = ""End SubPrivate Sub Command1_Click(Index As Integer) Select Case Index Case 0 Clipboard.Clear: Clipboard.SetText Text1.SelText Text1.SelText = "": Label1.Caption = "请选择要粘贴的位置!" Command1(0).Enabled = False: Command1(1).Enabled = False Command1(2).Enabled = True Case 1 Clipboard.Clear: Clipboard.SetText Text1.SelText Label1.Caption = "请选择要粘贴的位置!": Command1(0).Enabled = False Command1(1).Enabled = False: Command1(2).Enabled = True Case Else Text1.SelText = Clipboard.GetText(): Form_Load Label1.Caption = "粘贴完成!" End SelectEnd SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, _X As Single, Y As Single) Dim n As Integer n = Text1.SelLength If n > 0 Then Label1.Caption = "选择了" & n & "个字,请选择下一步操作!" Command1(0).Enabled = True: Command1(1).Enabled = True Else Label1.Caption = "" End IfEnd Sub程序6界面设计如图10-4所示,过程设计如下:Private Sub Form_Load() Label1.Caption = " 当前日期:": Text1.Text = Date Option1(0).Caption = "显示日期": Option1(1).Caption = "显示时间" Option1(0).Value = True: Command1.Caption = "退出"End SubPrivate Sub Option1_Click(Index As Integer) Select Case Index Case 0 Label1.Caption = "当前日期:": Text1.Text = Date Case 1 Label1.Caption = "当前时间:": Text1.Text = Time End SelectEnd SubPrivate Sub Command1_Click() EndEnd Sub程序7过程设计如下:Private Sub Form_Load() Label1.Caption = " 当前日期:": Text1.Text = Date Option1(0).Caption = "显示日期": Option1(1).Caption = "显示时间" Option1(0).Value = True: Command1.Caption = "退出" Combo1.AddItem "年-月-日": Combo1.AddItem "月-日-年" Combo1.AddItem "日-月-年": Combo1.Text = "年-月-日"End SubPrivate Sub Combo1_Click() Dim n As String Select Case Combo1.ListIndex Case 0: n = "yyyy-mm-dd" Case 1: n = "mm-dd-yyyy" Case 2: n = "dd-mm-yyyy" End Select Text1.Text = Format(Date, n)End SubPrivate Sub Option1_Click(Index As Integer) Select Case Index Case 0: Label1.Caption = "当前日期:": Text1.Text = Date Case 1: Label1.Caption = "当前时间:": Text1.Text = Time End SelectEnd SubPrivate Sub Command1_Click() EndEnd Sub程序8. 界面设计如图10-5所示,过程设计如下:Private Sub Form_Load() List1.AddItem "happy": List1.AddItem "apple" List1.AddItem "student": List1.AddItem "computer"End SubPrivate Sub Command1_Click() List1.AddItem Text1.Text: Text1.SetFocus Text1.SelStart = 0: Text1.SelLength = Len(Text1.Text)End SubPrivate Sub Command3_Click() List1.ClearEnd SubPrivate Sub Command4_Click() EndEnd SubPrivate Sub Command2_Click() If List1.ListIndex <> -1 Then List1.RemoveItem List1.ListIndexEnd Sub9界面设计如图10-6所示,过程设计如下:Private Sub Form_Load() Option1(0).Caption = "八进制": Option1(1).Caption = "十六进制" Option1(2).Caption = "二进制"End SubPrivate Function tran(k As Long, n As Integer) As String Dim x As Integer, y As String * 1, k1 As Long tran = "": k1 = Abs(k) Do While k1 > 0 x = k1 Mod n If x >= 10 Then y = Chr(Asc("A") + x - 10) Else y = CStr(x) tran = y + tran k1 = k1 n Loop If k < 0 Then tran = "-" + tranEnd FunctionPrivate Sub Option1_Click(Index As Integer) Dim k As Long, n As Integer k = Val(Text1.Text) Select Case Index Case 0 Label2.Caption = "八进制": n = 8 Case 1 Label2.Caption = "十六进制": n = 16 Case 2 Label2.Caption = "二进制": n = 2 End Select Text2.Text = tran(k, n)End Sub10界面设计如图10-7所示,过程设计如下:Private Sub Form_Load() Label1.Left = Width / 2 - Label1.Width / 2 Label1.AutoSize = True HScroll1.Min = 1: HScroll1.Max = 1000 HScroll1.SmallChange = 10: HScroll1.LargeChange = 100 HScroll1.Value = 500: Timer1.Interval = 500End SubPrivate Sub HScroll1_Change() Timer1.Interval = HScroll1.ValueEnd SubPrivate Sub HScroll1_Scroll() Call HScroll1_ChangeEnd SubPrivate Sub Timer1_Timer() Label1.FontSize = Label1.FontSize + 2 Label1.Left = Width / 2 - Label1.Width / 2 If Label1.FontSize >= 72 Then Timer1.Enabled = FalseEnd Sub第6章 图形控件和图形方法一、判断题1 2× 3 4× 5 6× 7× 8× 9 10二、选择题1C 2B 3C 4A 5A 6C 7A 8C 9C 10B11A 12B 13B 14C 15C三、填空题1 Circle (ScaleLeft + ScaleWidth / 2, ScaleTop + ScaleHeight / 2), 8002LoadPicture 3AutoSize、Stretch、False、False 4选中、属性5形状、矩形 6Picture1.Picture=LodePicture("C:WindowsCloud.bmp"