1、VB书后习题解答苏大出版社第五章 选择分支与循环1阅读程序,写出执行结果。(1) 6 55(2)运行结果为: DDEFEDDEFFEDDEF(3)运行结果为: 6 5(4)运行结果为: 11 10 82.编写程序,随机生成100个两位整数,并统计出其中小于等于40、大于40且小于等于70及大于70的数据个数。答:程序代码如下:Option ExplicitPrivate Sub Form_Click() Dim i As Integer, x As Integer Dim c1 As Integer, c2 As Integer, c3 As Integer For i = 1 To 100
2、x = Int(Rnd * 90) + 10 Print x; If i Mod 10 = 0 Then Print If x = 40 Then c1 = c1 + 1 ElseIf x = 70 Then c2 = c2 + 1 Else c3 = c3 + 1 End If Next i Print Print 40且70的数有:, c3End SubX00x2253.编写程序,求下面函数的值。 Option ExplicitPrivate Sub Command1_Click() Text1.Text = Text2.Text = Text1.SetFocusEnd SubPrivat
3、e Sub Command2_Click() Dim x As Integer x = Val(Text1.Text) Select Case x Case Is 5 Text2.Text = CStr(25 - x) Case Is 2 Text2.Text = CStr(x 2) Case Is 0 Text2.Text = CStr(x + 2) Case Else Text2.Text = CStr(2 - x) End SelectEnd Sub4.随机生成20个三位正整数,将其中的奇数和偶数分两行显示在窗体上。答:程序代码如下:Option ExplicitOption Base
4、1Private Sub Form_Click() Dim a(20) As Integer, i As Integer Print 生成的20个数如下: For i = 1 To 20 a(i) = Int(Rnd * 900) + 100 Print a(i); If i Mod 10 = 0 Then Print Next i For i = 1 To 20 If a(i) Mod 2 = 0 Then List2.AddItem a(i) Next i For i = 1 To 20 If a(i) Mod 2 = 1 Then List1.AddItem a(i) Next i Pr
5、intEnd Sub5.Private Sub Command1_Click() x = Val(Text1.Text) y = Val(Text2.Text) For i = x To x * y If i Mod x = 0 And i Mod y = 0 Then Text3.Text = CStr(i) Exit For End If Next iEnd Sub第六章 数 组1.6 7 92 5 81 3 42. 一维数组对调Option ExplicitOption Base 1Dim a(15) As IntegerPrivate Sub Command1_Click()Dim i
6、 As IntegerRandomizeFor i = 1 To 15a(i) = Int(Rnd * 100) + 1Text1.Text = Text1.Text + Format(a(i), 00) + Space(2)Next iEnd SubPrivate Sub Command2_Click()Dim i As Integer, t As IntegerFor i = 1 To UBound(a) / 2t = a(i)a(i) = a(UBound(a) + 1 - i)a(UBound(a) - i + 1) = tNext iFor i = 1 To 15Text2.Text
7、 = Text2.Text + Format(a(i), 00) + Space(2)Next iEnd SubPrivate Sub Command3_Click()Text1.Text = : Text2.Text = : Text1.SetFocusEnd SubPrivate Sub Command4_Click()Unload MeEnd Sub5.求二维数组的行和与列和Option ExplicitOption Base 1Dim a(4, 5) As IntegerDim SumL As Integer, SumC As IntegerPrivate Sub CmdArry_Cl
8、ick()Dim I As Integer, J As IntegerRandomizeFor I = 1 To 4For J = 1 To 5 a(I, J) = Int(Rnd * 9) + 1Picture1.Print a(I, J);Next JPicture1.PrintNext IEnd SubPrivate Sub CmdClear_Click()Picture1.Cls: Text1.Text = : Text2.Text = End SubPrivate Sub CmdColumn_Click()Dim I As Integer, J As IntegerFor J = 1
9、 To 5SumC = 0For I = 1 To 4SumC = SumC + a(I, J)Next IText2.Text = Text2.Text & SumC & Next JEnd SubPrivate Sub CmdExitCmdClear_Click()Unload MeEnd SubPrivate Sub CmdLine_Click()Dim I As Integer, J As IntegerFor I = 1 To 4SumL = 0For J = 1 To 5SumL = SumL + a(I, J)Next JText1.Text = Text1.Text & Sum
10、L & Chr(13) & Chr(10)Next IEnd Sub8.二维数组元素之和Option ExplicitDim a(5, 5) As Integer, I As Integer, J As IntegerDim s1, s2, s3 As IntegerPrivate Sub Command1_Click()Picture1.Print 生成数组元素:RandomizeFor I = 0 To 4For J = 0 To 4 a(I, J) = Int(Rnd * 90) + 10Picture1.Print Right( & a(I, J), 3);Next JPicture1
11、.PrintNext IEnd SubPrivate Sub Command2_Click()s1 = 0: s2 = 0: s3 = 0For I = 0 To 4 For J = 0 To 4 s1 = s1 + a(I, J) If I = 0 Or J = 0 Or I = 4 Or J = 4 Then s2 = s2 + a(I, J) If I = J Or I + J = 4 Then s3 = s3 + a(I, J)Next INext JText1 = s1: Text2 = s2: Text3 = s3End SubPrivate Sub Command3_Click(
12、)Picture1.Cls: Text1 = : Text2 = : Text3 = End SubPrivate Sub Command4_Click()Unload MeEnd Sub10. 杨辉三角形Option ExplicitPrivate Sub Form_click()Dim a() As Integer, N As IntegerDim m As Integer, i As Integer, j As IntegerDim q As StringN = InputBox(请输入N=?)q = Space(2 * N + 2)Print Left(q, 2 * N - 4) +
13、ReDim a(N, N)For m = 1 To Na(m, m) = 1: a(m, 1) = 1Next mFor i = 3 To N For j = 2 To i - 1 a(i, j) = a(i - 1, j - 1) + a(i - 1, j) Next jNext iFor i = 1 To N For j = 1 To i If j = 1 Then Print Right(q, 2 * N - 2 * i) & Right(Space(4) & a(i, j), 4); Else Print Right(Space(4) & a(i, j), 4); End If Nex
14、t j PrintNext iEnd Sub第七章 过 程1.在过程中声明的局部变量和在过程中声明的静态变量有什么不同?答:局部变量离开过程后,该变量的值就消失,而静态变量离开过程后,该变量的值不消失,下次调用时该变量保留上一次的值。2.在VB程序中调用程序向过程传递数据有哪几种方式?它们之间有什么不同?答:在VB程序中调用程序向过程传递数据有两种:按值传递与按地址传递。按值传递是指调用时将实参的值复制给形参,形参的值改变与实参没有任何关系;按地址传递是指调用时实参与形参共用同一个存储单元,形参的值发生变化实参的值也发生变化,实参的值发生变化,形参的值也发生变化。4.(1)输出结果为:BASI
15、C (2)输出结果为:6 262 62 -2 98(3) 输出结果为:23 47(4) 输出结果为:10 812 -105 212 -1010.Option ExplicitOption Base 1Dim A(5, 5) As IntegerPrivate Sub Command1_Click()Dim I As Integer, J As IntegerFor I = 1 To 5 For J = 1 To 5 A(I, J) = Int(Rnd * 90) + 10 Picture1.Print A(I, J); Next J Picture1.PrintNext IText1.Text
16、 = End SubPrivate Sub Command2_Click()Dim I As Integer, J As Integer, n1 As Integer, n2 As Integer, Max As IntegerText1.Text = n1 = 1: n2 = 1Max = A(n1, n2)For I = 1 To 5 For J = 1 To 5If Max aa(j + 1) Then t = aa(j) aa(j) = aa(j + 1) aa(j + 1) = t End If Next j Next i For i = 1 To 10 Text2.Text = T
17、ext2.Text & aa(i) & c = Text2.Text Next iEnd SubPrivate Sub Command3_Click() Open App.Path + data1.txt For Output As #2 Print #2, c Close #2End Sub界面:4、有一个名为“Score”的文本文件,每行记录一个学生的考试成绩,数据排列方式是:学号,姓名,英语成绩,数学成绩,计算机成绩。要求编写程序,实现在每个数据行后面添加该学生的总分和平均成绩两个数据项。程序:Dim k As Integer, xh() As String * 2, xm() As S
18、tring * 3, ecj() As IntegerDim mcj() As Integer, ccj() As Integer, sum() As Integer, ave() As IntegerPrivate Sub Form_Click() Open App.Path + score1.txt For Output As 10# For i = 1 To k - 1 Write #10, xh(i), xm(i), ecj(i), mcj(i), ccj(i), ecj(i) + mcj(i) + ccj(i), (ecj(i) + mcj(i) + ccj(i) / 3 Next
19、i Close 10 Kill App.Path + score.txt Name App.Path + score1.txt As App.Path + score.txtEnd SubPrivate Sub Form_Load() Open App.Path + score.txt For Input As #1 k = 1 Do While Not EOF(1) ReDim Preserve xh(k), xm(k), ecj(k), mcj(k), ccj(k) Input #1, xh(k), xm(k), ecj(k), mcj(k), ccj(k) k = k + 1 Loop
20、Close 1End Sub第10章习题解答1、组合框控件和列表框控件相比,在使用上有何特点? 列表框控件(ListBox)用于显示项目列表,用户可从中选择一个或多个项目。如果项目总数超过了可显示的项目数,VB会自动加上滚动条。列表框有两种风格:标准和复选列表框。通过它的Style属性来设置组合框控件(ComboBox)将文本框和列表框的功能结合在一起,用户可以在列表中选择某项(只能选取一项)。或在编辑区域中直接输入文本内容来选定项目。组合框共三种风格:下拉式组合框、简单组合框和下拉式列表框。2、滚动条控件的主要应用是什么?模仿例10-1,编写一个数量指示器程序,数量的变化范围是0100,在文本框中显示数量值。 在项目列表很长或者信息量很大时,可以使用滚动条提供简便的定位,也可用来作为渐变数据的输入工具,或数量、速度的指示器。程序清单:Private Sub HScroll1_Scroll() Text1.Text = HScroll1.ValueEnd SubPrivate Sub VSc roll1_Change() Text1.Text = VScroll1.Value End Sub界面:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1