VB程序设计教程实验指导与习题解答上机答案
【参考文档】vb实验指导书答案-范文word版 (10页)
第6章 过程
一、单选题
二、填空题 2.5 5. 工具 7. &,Click 8. Enabled 9. 窗体
10. 模式,无模式
11. Action
12. CommonDialog1.FileName 13. cdlCCRGBInit 14. Unload,Show 15. 所在窗体的名称 16. 打开 17. Visible 18. 当前 19. 255 20. Show
(6)强制显式声明变量 (7)类 P35 第三章
1. CBAAABCDAD ABBBC ADDBD 2.(1)integer (2)???? (3)text (4)a=0 or b=0 (5)(x+y)*sin(30*3.14/180) (6)int(rnd*100+1)(7)# (8)0
1
C
(9)int(rnd*30+70)(10) CDEF P53 第四章
本文部分内容来自网络整理,本司不为其真实性负责,如有异议或侵权请及时联系,本司将立即删除!
== 本文为word格式,下载后可方便编辑和修改! ==
vb实验指导书答案
篇一:VB程序设计学习与实验指导书答案(包含实验答案)
VB学习与实验指导测试题
参考答案
第一章
第二章
第三章
第四章
第五章
第六章
第七章
第八章
篇二:VB实验指导书答案
vb程序设计实验教程习题答案(精品).doc
实验教材实验-1:单项选择题:AACCC DCBBC2:填空题:(1).bas(2).vbp, ,frm(3)视图工具箱(4)对象(5)属性对象事件方法(6)Windows(7)事件行为(8)窗体模块实验二1:单项选择题:ADCCA BAB DC BBCBC BAB DC 2:基本运算题:(1)27(2) 3.33333333333333(3) 3(4)0(5)-3(6) 2(7)ab33(8)类型不匹配(9) 3(10)AB3实验三1:单项选择题:BCBDC2:填空题:(1)300(2)321456(3)Shanghai(4) 6 7 6(5)True(6)2+3=5(7)”area=” 12.56(8)VISUAL实验四1:单项选择题:CAACA2:填空题:(1)7(2) 3 5(3)输入的数小于0(4) 3(5)False(6)2010 is not leap year.试验五1:单项选择题:ACCAB ACCCB2:填空题:(1)9(2)@n ② 1(3)®Len(a)②n\2 ③Mid(a,(n-i+l),l)④Mid(a,(n-i+l),l)(4)@n Mod 3=1 And n Mod 5=1 And n Mod 7=1 ©While CountN<5(5)Mid(cl,l,i)实验六1:单项选择题:DB DCD ACADD2:填空题:(1)30(2) 3(3)①a(2)②a(3)③a(l)(4)①1 ②2 ③a(i)=a(6-i)(5)①i+j ②0 ③a(i,j)(6)©Print ②k ③a(k,j)=a(j,k)④a(k,j);实验七1:单项选择题:ABBACB2:填空题:(1)15(2)①Preserve a(n+l) ②a(i+l)=a(i) ③Exit For ④a(i+l)=m⑤0 to n+1 (或LBound(a) to UBound(a))(3)d,c,b,a实验八1:单项选择题:CADBD CACB2:填空题:(1)a=9 b=4(2)362516941(2)①a (2)UBound(b)③实验九1:单项选择题:DDDBB CAB CAD2:填空题:(1)①p=p*n ②Fac(n) ③ 10人-6(2)①Max=x ②Max=y ③p ④Max(x,a(n))(3)①number(i) ②n as integer (3)number=number+7* 10A(i-1)实验十1:单项选择题:DC BAB C实验十一1:单项选择题:DADBBB BDD实验十二1:单项选择题:CBCBC CBDAD DBC2:填空题:(1)①Label 1. FondName=Combo 1 .TextCombo2.Text ③ Label 1 .FontBold=True ④ Label 1 .FondItalic=True⑤ Label 1 .FondSize=Combo3.Text(2)①Listl.Text ©List 1 .RemoveitemListl.ListindexListl .Additem List2.Text ④ List2.RemoveItem实验十三实验十四1:单项选择题:DACDAABC2:填空题:(1)ShowFont(2)打开另存为颜色字体打印机帮助(3)工程部件Microsoft Common Dialog Control 6.0(4 ) ShowColor Action=3实验十五1:单项选择题:CB BCB B BAD2:填空题:(1)内建菜单弹出菜单(2)Click(3)工具(4)代码窗口Click(5)相同不同实验十六1:单项选择题:B CAAC ACDDB DCC2:填空题:(1)①Open "c:\studl.txt” For Output As #1Textl.Text=”End”White #l,Textl.Text(2)①For Input ②For Output As #2 ③Not Eof(l)Input #l,strl ⑤ Close #1,#2 ⑥Kill(“c:\old.txt”(3)①For Output ②For Input As #2。
vb程序设计书习题和参考答案
vb程序设计书习题和参考答案练习题习题⼀⼀、判断题1.Visual Basic是以结构化的Basic语⾔为基础、以事件驱动作为运⾏机制的可视化程序设计语⾔。
2.⾯向对象的程序设计是⼀种以对象为基础,由事件驱动对象的编程技术。
3.事件是由Visual Basic预先定义的对象能够识别的动作。
4.新建⼯程时系统会⾃动将窗体标题设置为Form1。
5.控件的属性值不可以在程序运⾏时动态地修改。
6.决定对象是否可见的属性为Visible属性,决定对象可⽤性的属性是Enabled属性。
7.保存Visual Basic⽂件时,若⼀个⼯程包含多个窗体或模块,则系统先保存⼯程⽂件,再分别保存窗体或模块⽂件。
8.单击“启动”菜单项将窗体装⼊内存时,⼀定触发窗体的Load事件。
9.“事件过程”是⽤来完成事件发⽣后所要执⾏的程序代码。
10.在打开某窗体时,初始化该窗体中的各控件,应选⽤Load事件。
⼆、选择题1.⼯程⽂件的扩展名为。
A、.frxB、.basC、.vbpD、.frm2.下列不属于对象的基本特征是。
A、属性B、⽅法C、事件D、函数3.在设计模式双击窗体将显⽰的窗⼝是A、⼯程窗⼝B、⼯具箱C、代码窗⼝D、属性窗⼝4.VB中运⾏程序的快捷键是A、F2B、F5C、Alt+F35.改变控件在窗体中的上下位置应修改该控件的属性。
A、TopB、LeftC、WidthD、Right6.窗体模块的⽂件扩展名是A、.exeB、.basC、.frxD、frm7.设置窗体的字体⽤属性。
A、FontNameB、FontSizeC、FontBoldD、FontItalic8.设置窗体的字号⽤属性。
A、FontNameB、FontSizeC、FontBoldD、FontItalic9.下列选项中不属于事件的是。
A、DblclickB、LoadC、ShowD、KeyUp10.VB编制的程序保存在磁盘上,⾄少会产⽣何种⽂件。
A、.doc与.txtB、.com与.exe.C、.bat与.frmD、.vbp与.frm三、程序设计题1.新建⼀个⼯程,单击窗体时,在窗体上显⽰“欢迎使⽤Visual Basic程序”。
vb程序设计教程课后答案与实验题答案-(第三版)资料
答案习题一答案在教材上找。
习题二1.对象指现实世界中的实体。
每个对象有自己的特征(属性)、行为(方法)和发生在该对象上的活动(事件)。
类是对同一种对象的集合与抽象。
类包含所创建对象的属性数据,以及对这些数据进行操作的方法。
封装和隐藏是类的重要特性,将数据结构和对数据的操作封装在一起,类的内部实现细节对用户来说是透明的。
VB工具箱中的可视图标是VB系统设计好的标准控件类。
通过将控件类实例化,得到真正的控件对象。
在窗体上画一个控件,就将类转换为对象,创建了一个控件对象。
VB中常用的对象是控件。
此外,VB还提供了系统对象。
窗体是个特例,它既是类也是对象。
当向一个工程添加一个新窗体时,实质就由窗体类创建了一个窗体对象。
对象的三要素:属性、事件和方法。
属性:存放对象的数据(决定对象外观及功能)对象属性的设置方法:在设计阶段,利用属性窗口直接设置属性值;程序运行时,通过语句[对象名.]属性名=属性值。
若属性只能在设计阶段设置,在程序运行阶段不可改变,称为只读属性。
事件:发生在对象上的事情。
同一事件,对不同的对象,会引发不同的反应。
事件过程:应用程序处理事件的步骤。
应用程序设计的主要工作就是为对象编写事件过程的代码。
方法:面向对象的程序设计语言,为程序设计人员提供了一种特殊的过程和函数。
方法是面向对象的,调用时一定要指明对象。
2.窗体和控件具有的基本属性“(1) Name属性:名称所创建的对象名称,在程序中引用。
(2) Caption属性:标题该属性决定了对象上显示的内容。
(3) Height、Width、Top和Left属性决定对象的大小和位置。
(4) Enabled 属性:对象是否允许操作(5) Visible 属性:对象是否可见(6) Font 属性组:文本的外观FontName字体、FontSize大小FontBold粗体、FontItalic斜体FontStrikethru 删除线FontUnderline 下划线(7) ForeColor属性:前景颜色(8) BackColor属性:背景颜色(9) MousePointer属性:鼠标指针类型, 设置值范围0~15,由系统指定。
vb程序设计教程习题答案
vb程序设计教程习题答案VB程序设计教程习题答案VB程序设计是一门广泛应用于软件开发领域的编程语言,它具有易学易用的特点,被广大程序员所喜爱。
在学习VB程序设计的过程中,习题是一个非常重要的环节,通过解答习题可以帮助我们巩固所学的知识,提高编程能力。
下面我将给出一些VB程序设计教程习题的答案,希望对初学者有所帮助。
1. 编写一个VB程序,实现计算两个数的和并输出结果的功能。
答案:```Public Class Form1Private Sub Button1_Click(sender As Object, e As EventArgs) HandlesButton1.ClickDim num1 As IntegerDim num2 As IntegerDim sum As Integernum1 = Integer.Parse(TextBox1.Text)num2 = Integer.Parse(TextBox2.Text)sum = num1 + num2MessageBox.Show("两个数的和为:" & sum)End SubEnd Class```2. 编写一个VB程序,实现判断一个数是奇数还是偶数的功能。
答案:```Public Class Form1Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickDim num As Integernum = Integer.Parse(TextBox1.Text)If num Mod 2 = 0 ThenMessageBox.Show("这个数是偶数")ElseMessageBox.Show("这个数是奇数")End IfEnd SubEnd Class```3. 编写一个VB程序,实现计算一个数的阶乘的功能。
VB实验教材的实验、习题答案
第一章VB开发环境与VB概述习题答案习题一、选择题第二章常用控件与界面设计习题答案习题一、选择题第三章实验与习题实验实验3_1…算术运算Private Sub Command1_Click()Const a As Single = 3, b As Single = 2, c As Single = -1, d As Single = 6Dim x As Single, y As Single, z As Single, m As Singlex = 2: y = 1: z = 1: m = 1List1.ClearList1.AddItem a / (b + c / b)List1.AddItem (x + Sqr(x ^ 2 + 1)) ^ (1 / 3)List1.AddItem ((2 * y) / (a * x + b * y) * (a * x - b * y))List1.AddItem 1 + 1 / (1 + 1 / m)List1.AddItem 1 / 2 * (d / 3) ^ (2 * x)List1.AddItem Log(y + Cos(x) ^ 2)List1.AddItem Abs((Exp(x) + Sin(x) ^ 3) / (x + y))List1.AddItem Log((Exp(x * y) + Abs(1 / Tan(z) + Cos(x) ^ 3)) / (x + y - z))List1.AddItem Sin(45 * 3.1415 / 180) + (Exp(10) + Log(10)) / Sqr(x + y + 1) List1.AddItem Log(a) / Log(b)End Sub…关系运算Private Sub Command2_Click()List1.ClearList1.AddItem Mid("Visual Basic", 1, 12) = Right("Language Visual Basic", 12) List1.AddItem "ABCRG" >= "abcde"List1.AddItem Int(134.69) >= CInt(134.9)List1.AddItem 78.9 / 32.77 <= 97.5 / 43.87 And -45.4 > -4.98List1.AddItem Str(32.345) = CStr(32.345)End Sub…逻辑运算Private Sub Command3_Click()Dim x As Integer, y As IntegerDim C1 As Integer, C2 As Integer, C3 As IntegerC1 = 70: C2 = 80: C3 = 90x = 9: y = -2List1.ClearList1.AddItem x + y < 10 And x - y > 0List1.AddItem (x% >= 0 And y% >= 0) Or (x% < 0 And y% < 0)List1.AddItem x = 0 Xor y = 0List1.AddItem C1 + C2 + C3 >= 255 Or C1 > 90 And C2 > 90 And C3 > 80End Sub实验3_2略实验3_3略实验3_4…零售结算Private Sub Command1_Click()Dim dj As Single, sl As Single, je As Single, n As IntegerLabel1.Caption = "金额:"dj = InputBox("请输入商品的单价:", "单价框")sl = InputBox("请输入商品的数量:", "数量框", 1)je = dj * sln = MsgBox("共计金额=" & je, vbOKCancel + vbInformation, "结果") If n = vbOK Then Label1.Caption = "金额:" & Str(je) & "元"End Sub…产生随机数Private Sub Command2_Click()Dim m As Integer, n As IntegerDim x1 As Integer, x2 As Integer, x3 As Integerm = V al(Text1.Text): n = Text2.TextRandomizex1 = Int(Rnd * (n - m + 1) + m)x2 = Int(Rnd * (n - m + 1) + m)x3 = Int(Rnd * (n - m + 1) + m)Label4.Caption = "结果为:" & Str(x1) & Str(x2) & Str(x3)End Sub…求逆序数Private Sub Command3_Click()Dim a As Integer, h As Integer, t As Integer, n As IntegerRandomizea = Int(Rnd * (999 - 100 + 1) + 100)h = Mid(CStr(a), 1, 1)t = Mid(CStr(a), 2, 1)n = Right(CStr(a), 1)Label5.Caption = a & "-->" & Format(n * 100 + t * 10 + h, "000")End Sub实验3_5略习题选择题填空题1、2 -32768~327672、整型长整型单精度双精度3、0 False “”4、Chr(Int(Rnd*(Asc(“L”)-Asc(“C”))+Asc(“C”)))x>0 And y>0 Or x<0 And y<0c>=”a” And c<=”z” Or c>=”A” And c<=”Z”x>=10 And x<20x<z Xor y<z5、False2TrueFalseTrueTrueTrueTrue6、18820010068.568765547、a=1 b=1a=2 b=1a=3 b=1a=4 b=18、Y Mod 4=0 And Y Mod 100<>0 Or Y Mod 400 =09、He Said, “Good morning”.10、EDEF11、(A>0 And A<>Int(A)) And (B<0 And B<>Int(B))12、a+b+c+d(a+b+c+d)/4sumaver13、(f-2*h)/2(4*h-f)/2简答题1、略2、Private Sub Form_Click()Const g = 9.8Dim V0 As Single, t As SingleDim S As SingleV0 = InputBox("请输入V0的值:")t = InputBox("请输入时间t的值:")S = (1 / 2) * g * t ^ 2 + V0 * tMsgBox ("位移量为:" & S)End Sub3、Private Sub Command1_Click()Dim l As Single, w As Single, h As SingleDim s As Singlel = Text1.Textw = Text2.Texth = Text3.Texts = (l * w + l * h + w * h) * 2Label1.Caption = sEnd Sub第四章选择分支与循环实验答案实验4-1方法一:x<=5 ; y方法二:Is<=2 ;Case Else实验4-2Shape1.BackColor=vbRed;Inter=15;Inter=Inter-1;Inter;实验4-3b<cb=aa=k实验4-4方法二:s*n ; t/s<10^-5实验4-5方法一:m>=10^-4 ; (-1)^(n+1)*m实验4-6x2=x3 ; <10^-6实验4-7实验4-8i+2*j+5*k=10 ; n实验4-9题目一:a) 5b)i=16 p=12 n=5c)i=9 p=8 n=1题目二:120 10实验4-10N=1 ;S=N& "*3 " & "+1 "实验4-11Len(st); Ch=Lcase(ch) ; Case Else ; Mid(Char,I,1)&St实验4-12i\100 ; (i Mod 100)\10习题一、选择题二、填空题1.Is=0 ; 1 To 10 ; Is >102. 93. 3 ; 4 154. this is a book ;This Is A Book5. 3 ; 3 ; 3 56. 32 07. 48. 15 19. 1 ; 401 ;10410. 1 2 3 ;1 3; 111. (3)12. 2 0 2413. 10 1414. BAABC CBAABC15. X Mod P=0 ; X=X+1 ;Find=False16. abs(t)<10^(-7) ; s+t17. Mid(n,1,1)=Mid(n,2,1) And Mid(n,3,1)=Mid(n,4,1)S=S+118. Mid(n,i,1)>Mid(n,i+1,1)19. "输入n";N/I=0 ;Print I第五章数组实验5_1Int(90 * Rnd) + 10a(i) = a(21 - i)a(21 - i) = t5_2k = x Mod 10i = i + 15_3s5_4max < a(i, j)row = Icol = j5_51ReDim Preserve prime(m)prime(j)5_6d Mod 25_7Exit Fora(j + 1) = a(j)5_8N – IA(j) = A(j + 1)5_9A(i, 1)JMin = MaxNot Flag5_10ch >= "0" And ch <= "9"P = P & chReDim Preserve C(k)5_11Private Sub Form_Click()Dim x As Integer, i As Integer, k As Integer, j As Integer Dim a() As Integerx = InputBox("输入正整数x=")i = 2Print x; "的质因子为:";Do While x >= iIf x Mod i = 0 Thenk = k + 1ReDim Preserve a(k)a(k) = ix = x \ iElsei = i + 1End IfLoopFor j = 1 To UBound(a)Print a(j);Next jEnd Sub习题一选择题二填空题1.V ariant2.ReDim A(N)A(1)If M < A(I) Then M = A(I)3.Int(26 * Rnd) + 65Chr(t)i + 1 To 10A(i) > A(j)4.InStr(s, ",")Right(s, Len(s) - n)s5. 4 5 66 5 43 2 16.T – 1I + 17.k = 6 - i – jmax = m8.j = aj = j - 39.i + 1i = i + 110. 237三编程题1.Option ExplicitOption Base 1Private Sub Command1_Click()Dim a(10) As IntegerDim i As Integer, max As IntegerFor i = 1 To 10a(i) = Int(90 * Rnd) + 10Print a(i);Next iPrinti = 1max = 0Do While i <= 10If a(i) Mod 3 = 0 ThenIf a(i) > max Thenmax = a(i)End IfEnd Ifi = i + 1LoopIf max = 0 ThenPrint "无要找的数"ElsePrint max & "是最大的能被3整除的数"End IfEnd Sub2.Option ExplicitOption Base 1Private Sub Command1_Click()Dim A() As IntegerDim I As Integer, m As IntegerDim K As IntegerReDim A(10)For I = 1 To 10A(I) = IPrint A(I);Next IPrintm = InputBox("ÊäÈëm")K = 9 + mReDim Preserve A(K)For I = 11 To KA(I) = A(I - 10)Next IFor I = 1 To 10A(I) = A(I + m - 1)Next IFor I = 1 To 10Print A(I);Next IPrintEnd Sub3.Option ExplicitPrivate Sub Command1_Click()Dim I As Integer, J As Integer, K As IntegerDim a() As Integer, n As Integer, p As IntegerDim s As String, Flag As Booleanp = 1For K = 10 To 100I = 1Do While I <= 10 And Not FlagJ = 1Do While J <= 10 And Not Flagn = I ^ 2 + J ^ 2If n = K ThenFlag = TrueReDim Preserve a(p)a(p) = Kp = p + 1s = I & Str(J) & Str(K)List1.AddItem sEnd IfJ = J + 1LoopI = I + 1LoopFlag = FalseNext KEnd Sub4.Option ExplicitPrivate Sub Command1_Click()Dim A(10) As IntegerDim B(10) As Integer, S As StringDim I As Integer, J As Integer, X As IntegerA(1) = Int(90 * Rnd) + 10S = A(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = A(J) Then Exit ForNext JIf J = I ThenA(I) = XS = S & " " & A(I)Exit DoEnd IfLoopNext IText1.Text = SB(1) = Int(90 * Rnd) + 10S = B(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = B(J) Then Exit ForNext JIf J = I ThenB(I) = XS = S & " " & B(I)Exit DoEnd IfLoopNext IText2.Text = SS = ""For I = 1 To 10For J = 1 To 10If A(I) = B(J) ThenS = S & " " & A(I)End IfNext JNext IText3.Text = SEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd Sub5.Option ExplicitPrivate Sub Command1_Click()Dim I As Integer, J As IntegerDim a() As Integer, K As Integer, Sum As Integer K = V al(Text1.Text)I = 1DoJ = K Mod 10ReDim Preserve a(I)a(I) = JSum = Sum + JK = K \ 10I = I + 1Loop Until K = 0Text2.Text = SumEnd Sub6.Option ExplicitPrivate Sub Command1_Click()Dim a(100, 2) As Integer, K As IntegerDim I As Integer, J As IntegerDim M As Integer, N As IntegerK = 1For I = 1 To 99For J = 1 To 99If I > J ThenM = I - JN = I + JIf Sqr(M) = Int(Sqr(M)) And Sqr(N) = Int(Sqr(N)) ThenList1.AddItem I & " " & Ja(K, 1) = Ia(K, 2) = JK = K + 1End IfEnd IfNext JNext IEnd Sub第六章参考答案实验实验6-11、Exit Do2、Y(X)3、X >= -1实验6-2【题目二】42 、26、8实验6-31、s1, key2、s As String3、ch >= "a" And ch <= "z"4、Mid(s, i, 1) = Chr(Asc(ch) + k - 26)思考:解密函数Private Sub dec(s As String, k As Integer) '解密过程Dim i As Integer, ch As String * 1For i = 1 To Len(s)ch = Mid(s, i, 1)If "A" <= ch And ch <= "Z" ThenIf Asc(ch) - k < Asc("A") ThenMid(s, i, 1) = Chr(Asc(ch) - k + 26) '小于A的ASCII值则加上26 ElseMid(s, i, 1) = Chr(Asc(ch) - k)End IfElseIf ch >= "a" And ch <= "z" Then '多分支格式,ch介于小写a与z之间If Asc(ch) - k < Asc("a") ThenMid(s, i, 1) = Chr(Asc(ch) - k + 26) '小于a的ASCII值则加上26ElseMid(s, i, 1) = Chr(Asc(ch) - k)End IfEnd IfNext iEnd Sub实验6-41、b() As Integer, st As String2、L <= R3、ReDim Preserve b(R)4、L = L + 1辗转相除法求最大公约数函数Private Function Gcd(ByV al p As Integer, ByV al q As Integer) As IntegerDim r As Integerr = p Mod qDo While r <> 0p = qq = rr = p Mod qLoopGcd = qEnd Function实验6-5【题目一】age = age(n - 1) - 3【题目二】1、(Left + Right) / 22、Left = Mid + 1实验6-61、3 -12、7 43、3 -14、7 0实验6-7【题目一】1、Sqr(2 + t)2、Abs(a - 1) < eps【题目二】1、Dim X As Integer2、"S(" & I & ")=" & S(I)3、(j * i + 1)习题参考答案一选择题二、填空1(1)形式、(2)实在2(1)按地址传递、(2)ByV al3(1)窗体模块、(2)标准模块、(3)类模块4(1) 65(1)4 20 1016(1) 6 26 、(2)-2 987(1)10 6 、(2) 5 -58(1) 6 5 、(2)8 79(1)11 、(2)710(1)Fun(i) 、(2)Fun=p11(1) 4 、(2)20.712(1)29 、(2)28.713(1)k = j 、(2)b() As Integer14(1)gec 、(2)ge 、(3) 315(1)p1 = True And p2 = True 或p1 And p2 、(2)p1 = p2 – 2 、(3)Isp = True 、(4)m Mod i = 016(1)Convert(st) 、(2)Len(s) 、(3)"A" To "F" 、(4)k = k + p * h17(1)isualVi 、(2)sualV isu18(1)s + a 、(2)n + 1 、(3)x As Single, n As Integer 、(4)p * (x + i) / ((2 * i - 1) * i * x) 三、改错题1、Option Base 1Private Sub Command1_Click()Dim A() As Integer, I As Integer, J As Integer, Logic As BooleanDim K As IntegerReDim A(1)A(1) = 50————————'K=1For I = 51 To 149K = 1 '位置错Logic = FalseCall Sub1(A, I, Logic)If Logic ThenK = K + 1ReDim A(K) 'ReDim Preserve A(K)A(K) = IEnd IfNext IFor I = 1 To UBound(A)Text1 = Text1 & Str$(A(I))If I Mod 5 = 0 Then Text1 = Text1 & vbCrLfNext IEnd SubPrivate Sub Sub1(A() As Integer, N As Integer, F As Boolean)Dim I As Integer, J As Integer, Ub As IntegerUb = UBound(A)For I = 1 To UbFor J = 2 To A(I)If A(I) Mod J = 0 And N Mod J = 0 ThenExit For 'Exit SubEnd IfNext JF = TrueNext IEnd Sub2、Option ExplicitOption Base 1Private Sub Command1_Click()Dim I As Integer, Flg As Boolean, S1 As Integer, S2 As IntegerDim J As Integer, K As Integer, P() As IntegerReDim P(1)P(1) = 2For I = 3 To 150 Step 2For J = 2 To Sqr(I)If I Mod J = 0 Then Exit ForNext JIf J > Sqr(I) ThenReDim Preserve P(UBound(P) + 1)P(UBound(P) + 1) = I 'P(UBound(P)) = IEnd IfNext IFor I = 80 To 125If fun(I, P, S1, S2) Then 'If fun(I, P, S1, S2)Text1 = Text1 & Str$(I) & "=" & Str(S1) & "*" & Str$(S2) & vbCrLf End IfNext IEnd SubPrivate Function fun(N As Integer, P() As Integer, S1 As Integer, S2 As Integer) As Boolean Dim I As Integer, J As IntegerFor I = 1 To UBound(P)For J = 1 To UBound(P) - 1If N = P(I) * P(J) ThenS1 = P(I)S2 = P(J)'fun = TrueExit Functionfun = True '位置错End IfNext JNext IEnd Function3、Option ExplicitPrivate Sub Get_Chess(Chess() As Integer, idx As Integer, Over As Integer) 'ByV al idx As IntegerDim Ub As Integer, Counter As Integer, K As IntegerUb = UBound(Chess)Chess(idx) = 0Counter = 1K = 0 '位置错Do Until Counter = Ub - 1Do Until K = 2idx = idx + 1If idx > Ub Then idx = 1K = K + Chess(idx)LoopChess(idx) = 0Counter = Counter + 1LoopFor Over = 1 To UbIf Chess(Over) <> 0 Then Exit ForNext OverEnd SubPrivate Sub Command1_Click()Dim Chess(32) As Integer, I As Integer, J As IntegerDim K As Integer, idx As IntegerFor I = 1 To 32For J = 1 To 32Chess(J) = 1Next JCall Get_Chess(Chess, I, K)If K = 23 ThenText1 = "从第" & Str$(I) & "号棋子开始取"Exit ForEnd IfNext IEnd Sub5、Option ExplicitPrivate Sub Form_Click()Dim S As String, Flg As BooleanDoS = InputBox("输入一个自然数")If S = "" Or Not IsNumeric(S) ThenMsgBox "请重新输入", 48 + vbOKOnlyElseExit DoEnd IfLoopjudge(S, Flg) ' Call judge(S, Flg) 或judge S, Flg If Flg ThenPrint S; "是回文数"ElsePrint S; "不是回文数"End IfEnd SubPrivate Sub judge(Ch As String, F As Boolean)Dim L As Integer, I As IntegerL = Len(Ch)F = True ' F = FalseFor I = 1 To LIf Mid$(Ch, I, 1) <> Mid$(Ch, L + 1 - I, 1) Then Exit Sub Next IF = False ' F = TrueEnd Sub四、编程题1、编程验证任一大于2的偶数可以表示成两个素数之和。
VB程序设计学习与实验指导答案-推荐下载
对全部高中资料试卷电气设备,在安装过程中以及安装结束后进行高中资料试卷调整试验;通电检查所有设备高中资料电试力卷保相护互装作置用调与试相技互术关,系电通,力1根保过据护管生高线产中0不工资仅艺料可高试以中卷解资配决料置吊试技顶卷术层要是配求指置,机不对组规电在范气进高设行中备继资进电料行保试空护卷载高问与中题带资2负料2,荷试而下卷且高总可中体保资配障料置2试时32卷,3各调需类控要管试在路验最习;大题对限到设度位备内。进来在行确管调保路整机敷使组设其高过在中程正资1常料中工试,况卷要下安加与全强过,看度并25工且52作尽22下可护都能1关可地于以缩管正小路常故高工障中作高资;中料对资试于料卷继试连电卷接保破管护坏口进范处行围理整,高核或中对者资定对料值某试,些卷审异弯核常扁与高度校中固对资定图料盒纸试位,卷置编工.写况保复进护杂行层设自防备动腐与处跨装理接置,地高尤线中其弯资要曲料避半试免径卷错标调误高试高等方中,案资要,料求编试技5写、卷术重电保交要气护底设设装。备备置管4高调、动线中试电作敷资高气,设料中课并技3试资件且、术卷料中拒管试试调绝路包验卷试动敷含方技作设线案术,技槽以来术、及避管系免架统不等启必多动要项方高方案中式;资,对料为整试解套卷决启突高动然中过停语程机文中。电高因气中此课资,件料电中试力管卷高壁电中薄气资、设料接备试口进卷不行保严调护等试装问工置题作调,并试合且技理进术利行,用过要管关求线运电敷行力设高保技中护术资装。料置线试做缆卷到敷技准设术确原指灵则导活:。。在对对分于于线调差盒试动处过保,程护当中装不高置同中高电资中压料资回试料路卷试交技卷叉术调时问试,题技应,术采作是用为指金调发属试电隔人机板员一进,变行需压隔要器开在组处事在理前发;掌生同握内一图部线纸故槽资障内料时,、,强设需电备要回制进路造行须厂外同家部时出电切具源断高高习中中题资资电料料源试试,卷卷线试切缆验除敷报从设告而完与采毕相用,关高要技中进术资行资料检料试查,卷和并主检且要测了保处解护理现装。场置设。备高中资料试卷布置情况与有关高中资料试卷电气系统接线等情况,然后根据规范与规程规定,制定设备调试高中资料试卷方案。
VB程序设计指导实验指导与习题解答上机答案
自测1程序代码Private Sub Command1_Click() Label1.FontSize = Label1.FontSize + 3End SubPrivate Sub Command2_Click() Label1.FontBold = TrueEnd SubPrivate Sub Command3_Click() Label1.FontUnderline = TrueEnd SubPrivate Sub Command4_Click() Label1.FontSize = 10Label1.FontBold = FalseLabel1.FontUnderline = FalseEnd Sub初始窗口:单机放大之后窗口:单机下划线之后窗口:单击加粗之后窗口:单击还原之后窗口:自测2程序代码Private Sub Command1_Click() Label1 = "visual basic"Text1 = "程序设计题"End SubPrivate Sub Command2_Click() Text1 = "visual basic"Label1 = "程序设计题"End Sub初始窗口:单击显示之后窗口:单击交换之后的窗口:自测题3Private Sub Command1_Click()Label1.FontSize = Label1.FontSize * (0.5) End SubPrivate Sub Command2_Click()Label1.Top = Label1.Top + 30Label1.Width = Label1.Width + 20Label1.Alignment = 1End Sub初始窗口:单击缩小两倍之后的窗口:单击向右下角移动之后的窗口:自测题4程序代码Private Sub Command1_Click() Form1.Caption = "VB学习" Print "简单的VB程序设计"End SubPrivate Sub Command2_Click() ClsEnd SubPrivate Sub Command3_Click() EndEnd SubPrivate Sub Command3_Click() EndEnd Sub初始化窗口:单击显示之后的窗口:单击清除之后的窗口:单击退出之后的窗口:自测题5程序代码Private Sub Command1_Click()Text1.Text = ""Text2.Text = ""End SubPrivate Sub Command2_Click()Text3.Text = Val(Text1.Text) + Val(Text2.Text) End SubPrivate Sub Command3_Click()EndEnd Sub初始化窗口:单击运算之后窗口:单击清除之后的窗口:单击退出之后的窗口:自测练习1Private Sub Command1_Click() Dim a$a = Text1.TextLabel1.Caption = Left(a, 2) End SubPrivate Sub Command2_Click() a = Text1.TextLabel2.Caption = Right(a, 2) End SubPrivate Sub Command3_Click()Text1.Text = Label1.Caption & Label2.Caption End Sub初始窗口界面输入字符串截取前两个字符截取后两个字符字符串连接自测练习2Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f%, g%, h%, i%, j%, k%, l% a = 7349e = a Mod 10 '个位d = (a \ 10) Mod 10 '十位c = (a \ 100) Mod 10 '百位b = a \ 1000 '千位i = (e + 7) Mod 10 '个位h = (d + 7) Mod 10 '十位g = (c + 7) Mod 10 '百位f = (b + 7) Mod 10 '千位j = ii = f '交换后的个位f = j '交换后的千位k = hh = g '交换后的十位g = k '交换后的百位l = f * 1000 + g * 100 + h * 10 + iText1.Visible = FalsePrint "加密前的4位整数是7439"; (Chr(10)); "加密后的结果是"; lEnd Sub初始界面单击窗体运行后的界面自测练习1Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f!a = Val(InputBox("输入第一个数", "输入一个数", 8))b = Val(InputBox("输入第一个数", "输入一个数", 7))c = a + bd = a - be = a * bf = a / bf = Fix(f * 100 + 0.5) / 100MsgBox "x+y=" & c & Chr(10) & "x-y=" & d & Chr(10) & "x*y=" & e & Chr(10) & "x/y=" & f, , "输出的结果"End Sub自测练习2Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f!a = Val(InputBox("输入第一个数的值"))b = Val(InputBox("输入第二个数的值"))c = Val(InputBox("输入第三个数的值"))d = Val(InputBox("输入第四个数的值"))e = a + b + c + df = e / 4Print "和="; e Print "平均值="; f End Sub自测练习3Private Sub Form_Click()Dim h%, f%, x%, y%h = Val(InputBox("输入总头数")) f = Val(InputBox("输入总脚数")) x = (4 * h - f) / 2y = (f - 2 * h) / 2Print "笼中鸡的头数为"; x; "只" Print "笼中兔的头数为"; y; "只"End Sub自测练习1:Private Sub Form_Click() Dim a%, b%, c%a = Val(InputBox("请输入第一条边", "边长"))b = Val(InputBox("请输入第二条边", "边长"))c = Val(InputBox("请输入第三条边", "边长"))If a + b > c And a + c > b And b + c > a ThenIf a * a + b * b = c * c Or a * a + c * c = b * b Or b * b = c * c = a * a ThenPrint "直角"ElseIf a = b And b = c ThenPrint "等边三角形"ElseIf a = b Or a = c Or b = c ThenPrint "等腰三角形"ElsePrint "一般三角形"End IfElsePrint "不是三角形"End IfEnd Sub不是三角形运行结果:初始窗口:输入三条边:运行结果:直角三角形运行结果:等边三角形运行结果如下:等腰三角形:一般三角形自测练习2:Private Sub Form_Click()Dim a%, b!a = Val(InputBox("请输入"))If a Mod 2 = 0 And a Mod 3 = 0 And a Mod 5 = 0 Then b = Sqr(a)Print a, bElseEndEnd IfEnd Sub初始化窗口:输入一个数:运行结果:自测练习3Private Sub Command1_Click()Dim a%, b%a = Val(Text1.Text)Select Case a > 0Case a < 20MsgBox "行李费为0.2元/kg", , "行李费"Case a >= 20 And a < 50MsgBox "行李费为0.3元/kg", , "行李费"Case a >= 50 And a < 100MsgBox "行李费为0.4元/kg", , "行李费"Case Elseb = 0.2 * 100 + (a - 100) * 0.5: MsgBox "行李费为" & b & "元", , "行李费"End SelectEnd Sub初始窗口:输入10:输入25:输入55:输入120:自测练习1Option Base 1Private Sub Form_Click()Dim i%, a(1 To 20) As Integera(1) = 1: a(2) = 1Print a(1);Print a(2);For i = 3 To 10a(i) = a(i - 1) + a(i - 2) Print a(i);Next iPrintFor i = 11 To 16a(i) = a(i - 1) + a(i - 2) Print a(i);Next iPrintFor i = 17 To 20a(i) = a(i - 1) + a(i - 2) Print a(i);Next i初始界面:运行结果界面:自测练习2Option Base 1Private Sub Form_Load() Form1.Width = 10000 Form1.Height = 5000 End SubPrivate Sub Command1_Click()Dim i%, j%, t%, a(1 To 4, 1 To 4) As Integer For i = 1 To 4For j = 1 To 4a(i, j) = Int(Rnd * 10) + 1Picture1.Print a(i, j);Next jPicture1.PrintNext iPicture3.Print "对角线的元素如下"For i = 1 To 4Picture3.Print a(i, i);Next iPicture3.PrintFor i = 1 To 4Picture3.Print a(i, 5 - i);Next iFor i = 1 To 4t = a(1, i)a(1, i) = a(4, i)a(4, i) = tNext iFor i = 1 To 4t = a(i, 2)a(i, 2) = a(i, 3)a(i, 3) = tNext iFor i = 1 To 4For j = 1 To 4Picture2.Print a(i, j); Next jPicture2.PrintNext iEnd Sub运行初始界面:运运行结果界面:自测练习1Sub itostr(a As Integer) Dim b%, c$a = Val(Text1.Text)While a >= 1c = Chr(a Mod 10 + 48) & ca = a \ 10WendText2.Text = c End SubPrivate Sub Command1_Click()Dim a As Integera = Val(Text1.Text)Call itostr(a)End Sub运行初始界面:运行结果界面:验证其他数字:自测练习2:Public Function area(n) As Double Dim c#, d#, e#, s#n = Val(Text2.Text)c = (1 - 0) / nWhile e <= 1d = I * c:e = (I + 1) * cs = s + (Sin(d) + Sin(e)) * c / 2 I = I + 1Wendarea = sEnd FunctionPrivate Sub Command1_Click() Dim s As DoubleCall area(n)Text1.Text = area(n)End Sub运行初始界面:运行结果:自测练习3Private Sub Form_click()n = Val(InputBox("请输入n的值"))x = Val(InputBox("请输入x的值"))Print "e的值="; es(n, x)End SubPrivate Function es(n, x)Dim a As Double, b As Double, c As Double, i As Integer a = 1: b = 1: c = 1For i = 1 To na = x * a:b = b * i:c = c + a / bNext ies = cEnd Function运行初始界面:输入数据:运行结果:自测练习4:For i = 1 To 4For j = i + 1 To 5If a(i) > a(j) Thent = a(i): a(i) = a(j): a(j) = tEnd IfNext jNext iFor i = 1 To 5Print a(i); " ";Next iFor i = 1 To 5If Y < a(i) Thent = a(i): a(i) = Y: Y = tEnd IfNext iFor i = 1 To 6a(6) = tNext iEnd FunctionPrivate Sub Command1_Click()Dim i#, a%(1 To 6), Y%Y = Val(InputBox("输入y的值", "输入数据")) Insertfun a%(), YPrintFor i = 1 To 6Print a(i); " "; Next iEnd Sub运行初始界面:插入数据:运行结果:41011060蓝志鹏资源1102 实验8自测练习1:Private Sub Command1_Click()If Ch1.Value = 1 ThenText1.Text = "报名参加羽毛球比赛"End IfIf Ch2.Value = 1 ThenText1.Text = "报名参加乒乓球比赛"End IfIf Ch1.Value = 1 And Ch2.Value = 1 Then Text1.Text = "报名参加羽毛球,乒乓球比赛" End IfEnd Sub运行初始界面:运行结果:自测练习4:Private Sub Form_Load() Label1.Alignment = 2 Label1.FontSize = 24 Label1.FontName = "黑体" Label1.ForeColor = &HFF&End SubPrivate Sub Option1_Click() Label1.Caption = "我是学生" End SubPrivate Sub Option2_Click() Label1.Caption = "我是教师" End SubPrivate Sub Option3_Click() Label1.Caption = "我是工人" End SubPrivate Sub Option4_Click() Label1.Caption = "我是农民" End Sub运行初始界面:运行结果:。
VB语言程序设计课后编程及上机调试参考答案
V B语言程序设计课后编程及上机调试参考答案ex0101Private Sub Label1_Click()Caption = "练习"= "确定"= True= True= "Visual Basic程序设计教程"End Subex0102Private Sub Command1_Click()= "我有电脑"End SubPrivate Sub Command2_Click()= "我没有电脑"End SubPrivate Sub Command3_Click()= "" '一对双撇号,中间不留空End Subex0104Private Sub Command1_Click()Dim x As Singlex = Val= 15 * xEnd Subex0202Private Sub Form_Click()Dim r As Single, s As Singler =s = * r ^ 2= "圆的面积为" & sEnd Subex0204Private Sub Command1_Click() '"最大化"按钮= 2 '最大化End SubPrivate Sub Command2_Click() '"还原"按钮= 0 '还原Private Sub Command3_Click() '"最小化"按钮= 1 '最小化End Subex0205Private Sub command1_click()Dim x As SingleDim y As Singlex = Valy = Val= x * x + y * yEnd Subex0206Private Sub command1_click()Dim x As Stringx == Left(x, 1) + Right(x, 1)End Subex0301Private Sub Form_Load()Dim x As Integer, y As IntegerDim a As Integer, b As Integer, c As Integerx = Val(InputBox("请输入一个三位数"))a = Int(x / 100) '求百位数b = Int(x / 10) - a * 10 '求十位数c = x Mod 10 '求个位数 Showy = c * 100 + b * 10 + a '生成倒序数 Print x; "倒序数为:"; y '显示倒序数End Subex0302Private Sub Command1_Click()n = ValRandomizex = Int(1 + n * Rnd)= "第一个随机数" + Str(x)x = Int(1 + n * Rnd)= "第二个随机数" + Str(x)x = Int(1 + n * Rnd)= "第三个随机数" + Str(x)ex0303Private Sub Cmd1_Click()= True= "您已下达显示命令"End SubPrivate Sub Form_Click()= TrueEnd Subex0304Private Sub Form_KeyPress(KeyAscii As Integer)ShowPrint "输入字符:"; Chr(KeyAscii), "ASCII码为;"; KeyAsciiEnd SubPrivate Sub Form_DblClick()ClsEnd Subex0305Private Sub Form_Load()x = MsgBox("在这里显示提示信息", 4 + 0, "请确认")x = MsgBox("在这里显示" + Chr(13) + "提示信息", 3 + 0, "请确认")x = MsgBox("在这里显示" + Chr(13) + "提示信息", 2 + 16 + 0, "请确认") End Subex0401Private Sub Form_Load()Dim x As Long, s As Stringx = InputBox("输入一个整数(1~35000)")s = "奇数"If x Mod 2 = 0 Then s = "偶数"MsgBox sEnd Subex0402Private Sub Command1_Click()Dim a As Single, b As SingleDim c As Single, d As SingleDim m As Single, n As Single, x As SingleShowa = Valb = Valc = Vald = ValIf a > b Then '求a、b中的较大数 m = aElsem = bEnd IfIf c > d Then '求c、d中的较大数 n = cElsen = dEnd IfIf m > n Then '求m、n中的较大数 x = mElsex = nEnd If= xEnd Subex0403Private Sub Command1_Click()Dim t As String, p As Stringt = Mid, 5, 1)Select Case tCase "2"p = "博士生"Case "3"p = "硕士生"Case "4"p = "本科生"Case "5"p = "专科生"Case Elsep = "无效学号"End Select= Left, 2) + "级"= pEnd Subex0404Private Sub Command1_Click()r = 0: g = 0: b = 0If = 1 Thenr = 255End IfIf = 1 Theng = 255End IfIf = 1 Thenb = 255End If= RGB(r, g, b)End SubPrivate Sub Command2_Click()EndEnd Subex0405Private Sub Form_Load()= "0"= 1= 1= 22= "黑体"= 1000End SubPrivate Sub TimClk_Timer()= Val + 1End Subex0406Private Sub Form_Load()= False= FalseEnd SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If > 0 Then= TrueEnd IfEnd SubPrivate Sub Command1_Click()= True= TrueEnd SubPrivate Sub Command2_Click()== False= FalseEnd Subex0501Private Sub Form_Load()Dim k As Integer, s As Integer, t As Integer s = 0For k = 1 To 100 Step 2s = s + kNext kShowPrint "1~100范围内的奇数和:"; sEnd Subex0502Private Sub Form_Load()Dim s As SingleShows = 0For k = 1 To 200s = s + k / (1 + k * k)Next kPrint Int(s * 10 + / 10End Subex0503Private Sub Form_Load()Showk = 2s = 0Do While k < 101s = s + k * (k - 1)k = k + 2LoopPrint sEnd Subex0504Private Sub Form_Load()Dim k As Integer, a As Integer, b As Integer Dim c As Integer, n As IntegerShown = 0Print "三位数的水仙花数是:"For k = 100 To 999a = Int(k / 100)b = Int((k - 100 * a) / 10)c = k Mod 10If a * a * a + b * b * b + c * c * c = k Then Print kn = n + 1End IfNext kPrint "一共有" & n & "个水仙花数"End Subex0505Private Sub Form_Load()ShowFor k = 1000 To 9999If k Mod 78 = 0 Thena = Int(k / 1000)b = Int(k / 100) - 10 * ac = Int(k / 10) - 100 * a - 10 * bd = k Mod 10If a = b And c = d ThenPrint kEnd IfEnd IfNext kEnd Subex0506Private Sub Command1_Click()Dim s As String, y As StringDim x As String, t As Strings = : y = ""For k = 1 To Len(s)x = Mid(s, k, 1)t = UCase(x)If t >= "A" And t <= "Z" Theny = y + xEnd IfNext k= yEnd Subex0507Private Sub Command1_click() '"显示"按钮单击事件FontSize = 12ClsFor i = 0 To - 1 '逐项判断If (i) Then '真时为选定Print (i)End IfNext iEnd Subex0508Private Sub Form_Load()ShowFor a = 1 To 9For b = 1 To 9For c = 1 To 9For d = 1 To 9x = a * 1000 + b * 100 + c * 10 + dy = b * 1000 + a * 100 + a * 10 + cz = d * 100 + d * 10 + aIf x - y = z ThenPrint "A=" & a & ",B=" & b & ",C=" & c & ",D=" & d End IfNext dNext cNext bNext aEnd Subex0601Private Sub Command1_Click()Dim a(10) As Integer, min As Integer, pos As IntegerRandomizeFor i = 1 To 10a(i) = Int(Rnd * 90 + 10)Print a(i);Next iPrintmin = a(1)pos = 1For k = 2 To 10If a(k) < min Thenmin = a(k)pos = kEnd IfNext kPrint "最小数:"; min, "位置:"; posEnd Subex0602Private Sub Form_Load()Dim a As Variant, b As VariantDim c(8) As Integera = Array(3, 4, 2, 1, 5, 7, 8, 11, 13)b = Array(10, 6, 12, 9, 13, 8, 8, 1, 16)For k = 0 To 8c(k) = a(k) + b(k)Next kShowFor k = 0 To 8Print c(k)Next kEnd Subex0603Private Sub Form_Load()Dim d() As IntegerDim k As Integer, t As Integer, m As Integer Dim h As Integer, n As Integern = Val(InputBox("请输入数组个数n:"))ReDim d(n)ShowPrint "处理前数据 :"RandomizeFor k = 1 To nd(k) = Int((101 * Rnd - 50))Print d(k);Next kPrintm = Int(n / 2)For k = 1 To mh = n - k + 1t = d(h): d(h) = d(k): d(k) = tNext kPrint "处理的结果 :"For k = 1 To nPrint d(k);Next kEnd Subex0604Private Sub Form_Load()Dim a(8, 8) As Integer, n As IntegerDim s As Long, i As Integer, j As IntegerShowRandomizen = 8For i = 1 To nFor j = 1 To nx = Int(90 * Rnd + 10) '产生一个随机数 a(i, j) = xNext jNext is = 0For i = 1 To nFor j = 1 To ns = s + a(i, j)Next jNext iPrint "所有元素之和;"; sPrintFor i = 1 To ns = 0For j = 1 To ns = s + a(i, j)Next jPrint "第"; i; "行元素之和;"; sNext iPrints = 0For i = 1 To ns = s + a(i, i)Next iPrint "主对角线元素之和;"; sPrints = 0For i = 1 To nFor j = 1 To nIf i = 1 Or i = n Or j = 1 Or j = n Thens = s + a(i, j)End IfNext jNext iPrint "所有靠边元素之和;"; sEnd Subex0605Option Base 1Private Sub Form_Load()Dim a(8, 8) As Integer, n As IntegerDim max As Integer, r As Integer, c As IntegerShowRandomizen = 8For i = 1 To nFor j = 1 To nx = Int(900 * Rnd + 100) '产生一个随机数 a(i, j) = xPrint x;Next jPrintNext imax = 0: r = 1: c = 1PrintFor i = 1 To nFor j = 1 To nIf a(i, j) > max Then '判断有无更大数 max = a(i, j): r = i: c = jEnd IfNext j, iPrint "最大值为:"; maxPrint "位于行:"; r, "列:"; cEnd Subex0606Dim n As IntegerPrivate Sub Cmd1_Click()n = n + 1If n = 3 Then n = 0Opt1(n).Value = TrueEnd SubPrivate Sub Form_Load()End Subex0607Private Sub Form_Load()Dim a(20) As IntegerRandomizea(1) = Int(10 + 90 * Rnd) k = 1Do While k < 20x = Int(10 + 90 * Rnd) f = 0For j = 1 To kIf x = a(j) Then f = 1Exit ForEnd IfNext jIf f = 0 Thenk = k + 1a(k) = xEnd IfLoopShowFor j = 1 To 20Print a(j)Next jEnd Subex0701Private Sub Form_Click()Dim s As Longs = 0For k = 1 To 10s = s + jc(k)Next kPrint sEnd SubFunction jc(n) As LongDim t As Longt = 1For k = 1 To nt = t * kNext kEnd Functionex0702Private Sub Form_Load()Dim a As Single, b As Single, c As SingleDim d As Single, m As Single, n As SingleShowa = Val(InputBox("输入第1个数"))b = Val(InputBox("输入第2个数"))c = Val(InputBox("输入第3个数"))d = Val(InputBox("输入第4个数"))m = max(a, b) '求a、b中的较大数 n = max(c, d) '求c、d中的较大数 Print "最大数为:" & max(m, n) '求m、n中的较大数End SubFunction max(x, y) As SingleIf x > y Thenmax = xElsemax = yEnd IfEnd Functionex0703Private Sub Command1_Click()Dim d(5) As Single, m As SingleFor k = 0 To 5d(k) = Val(Text1(k).Text)Next kCall Max(d(0), d(1), d(2), m)Call Max(m, d(3), d(4), m)Call Max(m, m, d(5), m)= mEnd Subex0704Private Sub Form_Load()Dim x As LongShowx = Val(InputBox("输入一个数"))Print x; "的位数为:"; fnws(x)End SubFunction fnws(x As Long) As IntegerDim n As Integern = 1Do While x \ 10 > 0n = n + 1x = x \ 10Loopfnws = nEnd Functionex0705Private Sub C1_Click()End Subex0705APrivate Sub C2_Click()End Subex0706Dim n As IntegerPrivate Sub Command1_Click()usern = Trimpassw = TrimIf usern = "username" And passw = "password" ThenElsen = n + 1If n = 3 ThenEnd IfEnd If= ""= ""End Subex0706APrivate Sub Command1_Click()End Subex0906BPrivate Sub Command1_Click()Unload Form1Unload Form2Unload Form3EndEnd Subex0802Private Sub Form_Load()Dim i As Integer, j As IntegerShowFor i = 1 To 12Print String(20 - i, 32); '输出当前行左边的空格,分号表示下一输出项紧接输出 For j = 1 To 2 * i - 1 '输出2*i-1个字符If j <= i Then '按左、右两边指定输出字符k = j '左边输出的字符Elsek = 2 * i - j '右边输出的字符End IfIf k > 9 Then k = k + 7 '字母的ASCII码比数字大7Print Chr(k + 48); '以字符格式输出Next jPrint '换行Next iEnd Subex0803Dim t As Single, f As IntegerPrivate Sub Command1_Click() '“产生原稿文”按钮Randomizes = ""For k = 1 To 30x = Chr(Int(Rnd * 26) + 97) '随机产生小写字母s = s + xNext k= s '显示在文本框中= ""= False '允许录入'设置焦点= ""= ""f = 0 '第1次按键标记,0表示未按键End SubPrivate Sub Command2_Click() '“结束”按钮EndEnd SubPrivate Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer)Dim c As Integer, k As IntegerIf f = 0 Then '第1次按键时,开始计时t = Timer '用t保存第1次按键的时间 f = 1 '1表示已按键和进入录入过程 End IfIf Len < 30 Then= Round(Timer - t, 1) & "秒" '显示用户当前所用时间Elsec = 0For k = 1 To 30 '统计录入正确的字母个数If Mid, k, 1) = Mid, k, 1) Thenc = c + 1End IfNext k= True '禁止录入= Round(c / 30 * 100, 2) & "%" '显示准确率End IfEnd Subex0901Private Sub Command1_Click()Open "静夜思.txt" For Input As #1Do While Not EOF(1) '文件未结束时,循环Input #1, xxLoopClose #1End Subex0902Dim d(1 To 20) As IntegerPrivate Sub Command1_Click() '"产生随机数"For i = 1 To 20d(i) = Int(1 + 99 * Rnd)d(i)Next iEnd SubPrivate Sub Command2_Click() '"保存"Open "" For Output As #1For i = 1 To 20Write #1, d(i)Next iClose #1End SubPrivate Sub Command3_Click() '"读出"Open "" For Input As #1Do While Not EOF(1)Input #1, xxLoopClose #1End SubPrivate Sub Command4_Click() '"结束"EndEnd Subex0903Private Sub Form_Load()ShowFor k = 1 To 6fn = "..\第7章\ex070" & k & ".vbp"If Dir(fn) = "" ThenPrint Mid(fn, 4) & "未编写!!"ElsePrint Mid(fn, 4) & "已编写!!"End IfNext kEnd Subex0904Private Sub Form_Load()chedir = "C:\Documents and Settings\Lin\My Documents\fsodir" If Dir(chedir, 16) = "" ThenMkDir chedirEnd Ifchefile = "C:\Windows\"If Dir(chefile) <> "" ThenFileCopy chefile, chedir & "\"End IfMsgBox "操作完毕!"End Subex1002Private Sub A1_Click()= vbRedEnd SubPrivate Sub A2_Click()= vbBlueEnd SubPrivate Sub A3_Click()= vbGreenEnd SubPrivate Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 ThenPopupMenu mun0End IfEnd Subex1003Private Sub Command1_Click()= "选择文件"= "文本文件|*.txt"= "C:\"=End SubPrivate Sub Command2_Click()Unload MeEndEnd Subex1101Private Sub calzc(x As Integer)= Round(2 * * x, 2)End SubPrivate Sub HV_Scroll()Dim r As Integerr == "=" & rCall calzc(r)End SubPrivate Sub Form_Load()= 300End Subex1102Private Sub Form_Load()For k = 0 To 3Image1(k).Stretch = TrueImage1(k).Picture = LoadPicture("箭头" & k + 1 & ".bmp") Next kEnd SubPrivate Sub Image1_Click(Index As Integer)= 20Select Case IndexCase 0"箭头向上"Case 1"箭头向左"Case 2"箭头向右"Case 3"箭头向下"End SelectEnd Sub。
VB语言程序设计课后编程及上机调试参考答案
V B 语言程序设计课后编程及上机调试参考答案ex0101Private Sub Label1_Click()Caption = " 练习 "Command1.Caption = " 确定 "Label1.FontBold = TrueLabel1.AutoSize = TrueLabel1.Caption = "Visual BasicEnd Sub ex0102Private Sub Command1_Click()Txt.Text = " 我有电脑 "End SubPrivate Sub Command2_Click()Txt.Text = " 我没有电脑 " EndSub程序设计教程Private Sub Command3_Click()Txt.Text = "" ' 一对双撇号,中间不留空 End Subex0104Private Sub Command1_Click()Dim x As Singlex = Val(Text1.Text)Text2.Text = 15 * xEnd Sub ex0202Private Sub Form_Click()Dim r As Single, s As Singler = 4.6s = 3.14159 * r A 2Text1.Text = " 圆的面积为 " & sEnd Subex0204Private Sub Command1_Click() '"Form1.WindowState = 2 ' End Sub最大化 " 按钮最大化Private Sub Command2_Click() '"Form1.WindowState = 0 '还原 "按钮还原End SubPrivate Sub Command3_Click() '"Form1.WindowState = 1 '最小化 " 按钮最小化End Sub ex0205Private Sub command1_click()Dim x As SingleDim y As Singlex = Val(Text1.Text)y = Val(Text2.Text)Text3.Text = x * x + y * y End Sub ex0206Private Sub command1_click()Dim x As Stringx = Text1.TextText2.Text = Left(x, 1) + Right(x, 1)End Sub ex0301 Private Sub Form_Load()Dim x As Integer, y As IntegerDim a As Integer, b As Integer, c As Integer x = Val(InputBox(" 请输入一个三位数 "))a = Int(x / 100) ' 求百位数b = Int(x / 10) - a * 10 ' 求十位数c = x Mod 10 ' 求个位数Showy = c * 100 + b * 10 + a ' 生成倒序数Print x; " 倒序数为: "; y ' 显示倒序数End Sub ex0302Private Sub Command1_Click()n = Val(Text1.Text)Randomizex = Int(1 + n *Rnd) Label1.Caption 第一个随机数" + Str(x)x = Int(1 + n *Rnd)Label2.Caption = " 第二个随机数" +Str(x)x = Int(1 + n *Rnd)Label3.Caption = " 第三个随机数" +Str(x)End Sub ex0303Private Sub Cmd1_Click()Lab1.Visible = TrueLab1.Caption = " 您已下达显示命令 "End SubPrivate Sub Form_Click()Cmd1.Visible = TrueEnd Subex0304Private Sub Form_KeyPress(KeyAscii As Integer)ShowPrint " 输入字符: "; Chr(KeyAscii), "ASCII 码为 ;"; KeyAscii End SubPrivate Sub Form_DblClick()ClsEnd Sub ex0305 Private Sub Form_Load()x = MsgBox(" 在这里显示提示信息 ", 4 + 0, " 请确认 ")x = MsgBox(" 在这里显示 " + Chr(13) + " 提示信息 ", 3 + 0, " x = MsgBox(" 在这里显示 " + Chr(13) + " 提示信息 ", 2 + 16 + 0,End Sub ex0401Private Sub Form_Load()Dim x As Long, s As Stringx = In putBox(" 输入一个整数(1 〜35000)")s = " 奇数"If x Mod 2 = 0 Then s = " 偶数"MsgBox s End Sub ex0402Private Sub Command1_Click()Dim a As Single, b As SingleDim c As Single, d As SingleDim m As Single, n As Single, x As SingleShowa = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)d = Val(Text4.Text)If a > b Then ' 求 a、b 中的较大数m = aElsem = bEnd IfIf c > d Then ' 求 c、d 中的较大数n = cElsen = dEnd IfIf m > n Then ' 求 m、n 中的较大数x = mElsex = nEnd IfText5.Text = x 请确认")请确认")End Subex0403Private Sub Command1_Click()Dim t As String, p As String t = Mid(Text1.Text, 5, 1) Select Case tCase "2"p = " 博士生 "Case "3"p = " 硕士生 "Case "4"p = " 本科生 "Case "5"p = " 专科生 "Case Elsep = " 无效学号 "End SelectText2.Text = Left(Text1.Text, 2) + "Text3.Text = pEnd Subex0404Private Sub Command1_Click()r = 0: g = 0: b = 0If Check1.Value = 1 Thenr = 255End IfIf Check2.Value = 1 Theng = 255End IfIf Check3.Value = 1 Thenb = 255End IfLabel1.BackColor = RGB(r, g, b)End SubPrivate Sub Command2_Click()EndEnd Subex0405Private Sub Form_Load()LabClk.Caption = "0"LabClk.Alignment = 1LabClk.BorderStyle = 1LabClk.FontSize = 22LabClk.FontName = " 黑体 "TimClk.Interval = 1000End SubPrivate Sub TimClk_Timer()LabClk.Caption = Val(LabClk.Caption) + 1End Sub ex0406 Private Sub Form_Load()Command1.Enabled = FalseCommand2.Enabled = FalseEnd SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Text1.SelLength > 0 ThenCommand1.Enabled = TrueEnd IfEnd SubPrivate Sub Command1_Click()Command1.Enabled = TrueCommand2.Enabled = TrueEnd SubPrivate Sub Command2_Click()Text2.Text = Text1.SelTextCommand1.Enabled = FalseCommand2.Enabled = FalseEnd Sub ex0501 Private Sub Form_Load()Dim k As Integer, s As Integer, t As Integers = 0For k = 1 To 100 Step 2s = s + kNext kShowPrint "1 〜100范围内的奇数和:";sEnd Sub ex0502 Private Sub Form_Load()Dim s As SingleShows = 0For k = 1 To 200s = s + k / (1 + k * k)Next kPrint Int(s * 10 + 0.5) / 10End Sub ex0503 Private Sub Form_Load()Showk = 2Do While k < 101s = s + k * (k - 1)k = k + 2LoopPrint sEnd Subex0504Private Sub Form_Load()Dim k As Integer, a As Integer, b As IntegerDim c As Integer, n As IntegerShown = 0Print " 三位数的水仙花数是: "For k = 100 To 999a = Int(k / 100)b = Int((k - 100 * a) / 10)c = k Mod 10If a * a * a + b * b * b + c * c * c = k Then Print kn = n + 1End IfNext kPrint " 一共有 " & n & " 个水仙花数 "End Sub ex0505Private Sub Form_Load()ShowFor k = 1000 To 9999If k Mod 78 = 0 Thena = Int(k / 1000)b = Int(k / 100) - 10 * ac = Int(k / 10) - 100 * a - 10 * bd = k Mod 10If a = b And c = d ThenPrint kEnd IfEnd IfNext kEnd Subex0506Private Sub Command1_Click()Dim s As String, y As StringDim x As String, t As Strings = Text1.Text: y = ""For k = 1 To Len(s)x = Mid(s, k, 1)t = UCase(x)If t >= "A" And t <= "Z" Theny = y + xEnd IfNext kText2.Text = yEnd Subex0507Private Sub Command1_click()FontSize = 12ClsFor i = 0 To List1.ListCount -1If List1.Selected(i) Then 'Print List1.List(i)End IfNext iEnd Subex0508Private Sub Form_Load()ShowFor a = 1 To 9For b = 1 To 9For c = 1 To 9For d = 1 To 9x = a * 1000 + b * 100 + c y = b * 1000 + a * 100 + a z = d * 100 + d * 10 + a If x - y = z ThenPrint "A=" & a & ",B=" & b & ",C=" & c & ",D=" & d End IfNext dNext cNext bNext aEnd Sub ex0601Private Sub Command1_Click()Dim a(10) As Integer, min As Integer, pos As Integer RandomizeFor i = 1 To 10 a(i) = Int(Rnd * 90 + 10) Print a(i);Next iPrintmin = a(1)pos = 1For k = 2 To 10 If a(k) < min Then min = a(k) pos = k 10 + d 10 + c 显示 "按钮单击事件逐项判断 真时为选定Next kPrint " 最小数: "; min, " 位置: "; posEnd Sub ex0602Private Sub Form_Load()Dim a As Variant, b As VariantDim c(8) As Integera = Array(3, 4, 2, 1, 5, 7, 8, 11, 13)b = Array(10, 6, 12, 9, 13, 8, 8, 1, 16)For k = 0 To 8 c(k) = a(k) + b(k)Next kShowFor k = 0 To 8Print c(k)Next kEnd Sub ex0603 Private Sub Form_Load()Dim d() As IntegerDim k As Integer, t As Integer, m As IntegerDim h As Integer, n As Integern = Val(InputBox(" 请输入数组个数 n: "))ReDim d(n)ShowPrint " 处理前数据 :"RandomizeFor k = 1 To nd(k) = Int((101 * Rnd - 50))Print d(k);Next kPrintm = Int(n / 2)For k = 1 To m h = n - k + 1 t = d(h): d(h) = d(k): d(k) = t Next kPrint " 处理的结果 :"For k = 1 To nPrint d(k);Next kEnd Subex0604Private Sub Form_Load()Dim a(8, 8) As Integer, n As IntegerDim s As Long, i As Integer, j As IntegerShown = 8For i = 1 To nFor j = 1 To nx = Int(90 * Rnd + 10) ' 产生一个随机数a(i, j) = xNext jNext is = 0For i = 1 To nFor j = 1 To ns = s + a(i, j)Next jNext iPrint " 所有元素之和; "; sPrintFor i = 1 To ns = 0For j = 1 To ns = s + a(i, j)Next jPrint " 第 "; i; " 行元素之和; "; sNext iPrints = 0For i = 1 To ns = s + a(i, i)Next iPrint " 主对角线元素之和; "; sPrints = 0For i = 1 To nFor j = 1 To nIf i = 1 Or i = n Or j = 1 Or j = n Then s = s + a(i, j)End IfNext jNext iPrint " 所有靠边元素之和; "; sEnd Sub ex0605 Option Base 1Private Sub Form_Load()Dim a(8, 8) As Integer, n As IntegerDim max As Integer, r As Integer, c As Integer Show Randomizen = 8For i = 1 To nFor j = 1 To nx = Int(900 * Rnd + 100) 'a(i, j) = xPrint x;Next jPrintNext imax = 0: r = 1: c = 1PrintFor i = 1 To nFor j = 1 To nIf a(i, j) > max Then 'max = a(i, j): r = i: c = jEnd IfNext j, iPrint " 最大值为: "; maxPrint " 位于行: "; r, " 列: "; c End Sub ex0606 Dim n As IntegerPrivate Sub Cmd1_Click()n = n + 1If n = 3 Then n = 0Opt1(n).Value = True End SubPrivate Sub Form_Load()n = 1End Sub ex0607Private Sub Form_Load()Dim a(20) As IntegerRandomizea(1) = Int(10 + 90 * Rnd)k = 1Do While k < 20x = Int(10 + 90 * Rnd) 产生一个随机数判断有无更大数For j = 1 To kIf x = a(j) Then f = 1 Exit ForEnd IfNext jIf f = 0 Thenk = k + 1 a(k) = xEnd IfLoopShowFor j = 1 To 20Print a(j)Next jEnd Sub ex0701Private Sub Form_Click() Dim s As Long s = 0For k = 1 To 10 s = s + jc(k)Next kPrint sEnd SubFunction jc(n) As LongDim t As Longt = 1For k = 1 To nt = t * kNext kjc = tEnd Functionex0702Private Sub Form_Load()Dim a As Single, b As Single, c As Single Dim d As Single, m As Single, n As Single Showa = Val(InputBox(" 输入第 1 个数 "))b = Val(InputBox(" 输入第 2 个数 "))c = Val(InputBox(" 输入第 3 个数"))d = Val(InputBox(" 输入第 4 个数 "))m = max(a, b) 'n = max(c, d) 'Print " 最大数为: " & max(m, n) End SubFunction max(x, y) As SingleIf x > y Then max = xElsemax = y 求a、 b 中的较大数求c、 d 中的较大数求m、n 中的较大End Function ex0703Private Sub Command1_Click()Dim d(5) As Single, m As SingleFor k = 0 To 5d(k) = Val(Text1(k).Text) Next kCall Max(d(0), d(1), d(2), m)Call Max(m, d(3), d(4), m)Call Max(m, m, d(5), m) Text2.Text = mEnd Sub ex0704Private Sub Form_Load() Dim x As Long Showx = Val(InputBox(" 输入一个数 "))Print x; " 的位数为: "; fnws(x) End SubFunction fnws(x As Long) As Integer Dim n As Integer n = 1 Do While x \ 10 > 0n = n + 1x = x \ 10 Loop fnws = nEnd Function ex0705Private Sub C1_Click()Form1.HideEnd Sub ex0705APrivate Sub C2_Click()Form1.ShowEnd Sub ex0706 Dim n As IntegerPrivate Sub Command1_Click()usern = Trim(Text1.Text)passw = Trim(Text2.Text)If usern = "username" And passw = "password" Then Form1.HideForm2.ShowElsen = n + 1Text1.SetFocusIf n = 3 ThenForm1.HideForm3.ShowEnd IfEnd IfText1.Text = ""Text2.Text = ""End Subex0706APrivate Sub Command1_Click()Form2.HideForm1.ShowEnd Subex0906BPrivate Sub Command1_Click()Unload Form1Unload Form2Unload Form3EndEnd Sub ex0802 Private Sub Form_Load()Dim i As Integer, j As IntegerShowFor i = 1 To 12Print String(20 - i, 32); ' For j = 1 To 2 * i - 1 'If j <= i Then 'k = j 'Elsek = 2 * i - j 'End IfIf k > 9 Then k = k + 7 ' Print Chr(k + 48); 'Next jPrint ' 输出当前行左边的空格,分号表示下一输出项紧接输出输出2*i-1 个字符按左、右两边指定输出字符左边输出的字符右边输出的字符字母的ASCII 码比数字大7 以字符格式输出换行End SubDim t As Single, f As IntegerPrivate Sub Command1_Click()RandomizeIII! For k = 1 To 30If Mid(Text1.Text, k, 1) = Mid(Text2.Text, k, 1) Then c = c + 1End IfNext k Text2.Locked = True ' 禁止录入Text4.Text = Round(c / 30 * 100, 2) & "%" ' 显示准确率End IfEnd Subex0901Private Sub Command1_Click()Open " 静夜思 .txt" For Input As #1Do While Not EOF(1) '文件未结束时,循环Input #1, xList1.AddItem xLoopClose #1End Sub ex0902 产生原稿文”按钮x = Chr(Int(Rnd * 26) + 97) 's = s + xNext kText1.Text = s 'Text2.Text = ""Text2.Locked = False 'Text2.SetFocus 'Text3.Text = ""Text4.Text = ""f = 0 'End SubPrivate Sub Command2_Click()' EndEnd SubPrivate Sub Text2_KeyUp(KeyCode As Integer,Dim c As Integer, k As IntegerIf f = 0 Then' t = Timer' f = 1'1 End IfIf Len(Text2.Text) < 30 ThenText3.Text = Round(Timer - t, 1) & "Elsec = 0For k = 1 To 30 ' 随机产生小写字母 显示在文本框中 允许录入 设置焦点 第 1 次按键标记, 0 表示未按键 “结束”按钮 As Integer) 第 1 次按键时,开始计时 用t 保存第1次按键的时间 表示已按键和进入录入过程 秒 " ' 显示用户当前所用时间统计录入正确的字母个数Dim d(1 To 20) As IntegerPrivate Sub Command1_Click() '" 产生随机数 "For i = 1 To 20 d(i) = Int(1 + 99 * Rnd) List1.AddItem d(i)Next iEnd SubPrivate Sub Command2_Click() '" 保存 "Open "MyFile4.txt" For Output As #1For i = 1 To 20Write #1, d(i)Next iClose #1End SubPrivate Sub Command3_Click() '" 读出 "Open "MyFile4.txt" For Input As #1Do While Not EOF(1) Input #1, x List2.AddItem xLoopClose #1End SubPrivate Sub Command4_Click() '" 结束 "EndEnd Sub ex0903 Private Sub Form_Load()ShowFor k = 1 To 6第 7 章\ex070" & k & ".vbp"If Dir(fn) = "" ThenPrint Mid(fn, 4) & " 未编写!! " ElsePrint Mid(fn, 4) & " 已编写!! " End IfNext kEnd Subex0904Private Sub Form_Load()chedir = "C:\Documents and Settings\Lin\My Documents\fsodir" If Dir(chedir, 16) = "" ThenMkDir chedirEnd Ifchefile = "C:\Windows\notepad.exe"If Dir(chefile) <> "" ThenFileCopy chefile, chedir & "\mynotepad.exe"End IfMsgBox " 操作完毕! "End Subex1002Private Sub A1_Click()Label1.ForeColor = vbRedEnd SubPrivate Sub A2_Click()Label1.ForeColor = vbBlueEnd SubPrivate Sub A3_Click()Label1.ForeColor = vbGreenEnd SubPrivate Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 ThenPopupMenu mun0End IfEnd Subex1003Private Sub Command1_Click()CommonDialog1.DialogTitle = " 选择文件 " CommonDialog1.Filter = " 文本文件 |*.txt"CommonDialog1.InitDir = "C:\"CommonDialog1.ShowOpenText1.Text = CommonDialog1.FileNameEnd SubPrivate Sub Command2_Click()Unload MeEndEnd Subex1101Private Sub calzc(x As Integer)Text2.Text = Round(2 * 3.14159 * x, 2)End SubPrivate Sub HV_Scroll()Dim r As Integer r = HV.ValueText1.Text = "=" & rCall calzc(r)End SubPrivate Sub Form_Load()HV.Max = 300End Subex1102Private Sub Form_Load()For k = 0 To 3 Image1(k).Stretch = TrueImage1(k).Picture = LoadPicture(" 箭头 " & k + 1 & ".bmp")Next kEnd SubPrivate Sub Image1_Click(Index As Integer) Picture1.ClsSelect Case IndexCase 0Picture1.Print "Case 1箭头向上 "Picture1.Print " Case 2Picture1.Print " Case 3 箭头向左 " 箭头向右 "Picture1.Print "End SelectEnd Sub箭头向下 "。
VB语言程序设计课后编程及上机调试参考答案
VB语言程序设计课后编程及上机调试参考答案VB语言程序设计课后编程及上机调试参考答案ex0101Private Sub Label1_Click()Caption = "练习"Command1.Caption = "确定"Label1.FontBold = TrueLabel1.AutoSize = TrueLabel1.Caption = "Visual Basic程序设计教程" End Subex0102Private Sub Command1_Click()Txt.Text = "我有电脑"End SubPrivate Sub Command2_Click()Txt.Text = "我没有电脑"End SubPrivate Sub Command3_Click()Txt.Text = "" '一对双撇号,中间不留空End Subex0104Private Sub Command1_Click()Dim x As Singlex = Val(Text1.Text)Text2.Text = 15 * xEnd Subex0202Private Sub Form_Click()Dim r As Single, s As Singler = 4.6s = 3.14159 * r ^ 2Text1.Text = "圆的面积为" & sEnd Subex0204Private Sub Command1_Click() '"最大化"按钮 Form1.WindowState = 2 '最大化End SubPrivate Sub Command2_Click() '"还原"按钮 Form1.WindowState = 0 '还原End SubPrivate Sub Command3_Click() '"最小化"按钮 Form1.WindowState = 1 '最小化End Subex0205Private Sub command1_click()Dim x As SingleDim y As Singlex = Val(Text1.Text)y = Val(Text2.Text)Text3.Text = x * x + y * yEnd Subex0206Private Sub command1_click()Dim x As Stringx = Text1.TextText2.Text = Left(x, 1) + Right(x, 1) End Subex0301Private Sub Form_Load()Dim x As Integer, y As IntegerDim a As Integer, b As Integer, c As Integer x = Val(InputBox("请输入一个三位数"))a = Int(x / 100) '求百位数b = Int(x / 10) - a * 10 '求十位数c = x Mod 10 '求个位数Showy = c * 100 + b * 10 + a '生成倒序数Print x; "倒序数为:"; y '显示倒序数End Subex0302Private Sub Command1_Click()n = Val(Text1.Text)Randomizex = Int(1 + n * Rnd)Label1.Caption = "第一个随机数" + Str(x)x = Int(1 + n * Rnd)Label2.Caption = "第二个随机数" + Str(x)x = Int(1 + n * Rnd)Label3.Caption = "第三个随机数" + Str(x) End Subex0303Private Sub Cmd1_Click()Lab1.Visible = TrueLab1.Caption = "您已下达显示命令"End SubPrivate Sub Form_Click()Cmd1.Visible = TrueEnd Subex0304Private Sub Form_KeyPress(KeyAscii As Integer) ShowPrint "输入字符:"; Chr(KeyAscii), "ASCII码为;"; KeyAsciiEnd SubPrivate Sub Form_DblClick()ClsEnd Subex0305Private Sub Form_Load()x = MsgBox("在这里显示提示信息", 4 + 0, "请确认")x = MsgBox("在这里显示" + Chr(13) + "提示信息", 3 + 0, "请确认")x = MsgBox("在这里显示" + Chr(13) + "提示信息", 2 + 16 + 0, "请确认")End Subex0401Private Sub Form_Load()Dim x As Long, s As Stringx = InputBox("输入一个整数(1~35000)")s = "奇数"If x Mod 2 = 0 Then s = "偶数"MsgBox sEnd Subex0402Private Sub Command1_Click()Dim a As Single, b As SingleDim c As Single, d As SingleDim m As Single, n As Single, x As Single Showa = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)d = Val(Text4.Text)If a > b Then '求a、b中的较大数m = aElsem = bEnd IfIf c > d Then '求c、d中的较大数n = cElsen = dEnd IfIf m > n Then '求m、n中的较大数x = mElsex = nEnd IfText5.Text = xEnd Subex0403Private Sub Command1_Click()Dim t As String, p As String t = Mid(Text1.Text, 5, 1)Select Case tCase "2"p = "博士生"Case "3"p = "硕士生"Case "4"p = "本科生"Case "5"p = "专科生"Case Elsep = "无效学号"End SelectText2.Text = Left(Text1.Text, 2) + "级" Text3.Text = pEnd Subex0404Private Sub Command1_Click()r = 0: g = 0: b = 0If Check1.Value = 1 Thenr = 255End IfIf Check2.Value = 1 Theng = 255End IfIf Check3.Value = 1 Thenb = 255End IfLabel1.BackColor = RGB(r, g, b)End SubPrivate Sub Command2_Click()EndEnd Subex0405Private Sub Form_Load()LabClk.Caption = "0"LabClk.Alignment = 1LabClk.BorderStyle = 1LabClk.FontSize = 22LabClk.FontName = "黑体"TimClk.Interval = 1000End SubPrivate Sub TimClk_Timer()LabClk.Caption = Val(LabClk.Caption) + 1 End Subex0406Private Sub Form_Load()Command1.Enabled = FalseCommand2.Enabled = FalseEnd SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)If Text1.SelLength > 0 ThenCommand1.Enabled = TrueEnd IfEnd SubPrivate Sub Command1_Click()Command1.Enabled = TrueCommand2.Enabled = TrueEnd SubPrivate Sub Command2_Click()Text2.Text = Text1.SelTextCommand1.Enabled = FalseCommand2.Enabled = FalseEnd Subex0501Private Sub Form_Load()Dim k As Integer, s As Integer, t As Integer s = 0For k = 1 To 100 Step 2s = s + kNext kShowPrint "1~100范围内的奇数和:"; sEnd Subex0502Private Sub Form_Load()Dim s As SingleShows = 0For k = 1 To 200s = s + k / (1 + k * k) Next kPrint Int(s * 10 + 0.5) / 10 End Subex0503Private Sub Form_Load()Showk = 2s = 0Do While k < 101s = s + k * (k - 1)k = k + 2LoopPrint sEnd Subex0504Private Sub Form_Load()Dim k As Integer, a As Integer, b As Integer Dim c As Integer, n As IntegerShown = 0Print "三位数的水仙花数是:"For k = 100 To 999a = Int(k / 100)b = Int((k - 100 * a) / 10)c = k Mod 10If a * a * a + b * b * b + c * c * c = k ThenPrint kn = n + 1End IfNext kPrint "一共有" & n & "个水仙花数"End Subex0505Private Sub Form_Load()ShowFor k = 1000 To 9999If k Mod 78 = 0 Thena = Int(k / 1000)b = Int(k / 100) - 10 * ac = Int(k / 10) - 100 * a - 10 * bd = k Mod 10If a = b And c = d ThenPrint kEnd IfEnd IfNext kEnd Subex0506Private Sub Command1_Click()Dim s As String, y As StringDim x As String, t As Strings = Text1.Text: y = ""For k = 1 To Len(s)x = Mid(s, k, 1)t = UCase(x)If t >= "A" And t <= "Z" Theny = y + xEnd IfNext kText2.Text = yEnd Subex0507Private Sub Command1_click() '"显示"按钮单击事件FontSize = 12ClsFor i = 0 To List1.ListCount - 1 '逐项判断 If List1.Selected(i) Then '真时为选定Print List1.List(i)End IfNext iEnd Subex0508Private Sub Form_Load()ShowFor a = 1 To 9For b = 1 To 9For c = 1 To 9For d = 1 To 9x = a * 1000 + b * 100 + c * 10 + d y = b * 1000 + a * 100 + a * 10 + c z = d * 100 + d * 10 + aIf x - y = z ThenPrint "A=" & a & ",B=" & b & ",C=" & c & ",D=" & dEnd IfNext dNext cNext bNext aEnd Subex0601Private Sub Command1_Click()Dim a(10) As Integer, min As Integer, pos As IntegerRandomizeFor i = 1 To 10a(i) = Int(Rnd * 90 + 10)Print a(i);Next iPrintmin = a(1)pos = 1For k = 2 To 10If a(k) < min Thenmin = a(k)pos = kEnd IfNext kPrint "最小数:"; min, "位置:"; posEnd Subex0602Private Sub Form_Load()Dim a As Variant, b As VariantDim c(8) As Integera = Array(3, 4, 2, 1, 5, 7, 8, 11, 13)b = Array(10, 6, 12, 9, 13, 8, 8, 1, 16)For k = 0 To 8c(k) = a(k) + b(k)Next kShowFor k = 0 To 8Print c(k)Next kEnd Subex0603Private Sub Form_Load()Dim d() As IntegerDim k As Integer, t As Integer, m As Integer Dim h As Integer, n As Integern = Val(InputBox("请输入数组个数n:"))ReDim d(n)ShowPrint "处理前数据 :"RandomizeFor k = 1 To nd(k) = Int((101 * Rnd - 50))Print d(k);Next kPrintm = Int(n / 2)For k = 1 To mh = n - k + 1t = d(h): d(h) = d(k): d(k) = tNext kPrint "处理的结果 :"For k = 1 To nPrint d(k);Next kEnd Subex0604Private Sub Form_Load()Dim a(8, 8) As Integer, n As IntegerDim s As Long, i As Integer, j As Integer ShowRandomizen = 8For i = 1 To nFor j = 1 To nx = Int(90 * Rnd + 10) '产生一个随机数a(i, j) = xNext jNext is = 0For i = 1 To nFor j = 1 To ns = s + a(i, j)Next jNext iPrint "所有元素之和;"; sPrintFor i = 1 To ns = 0For j = 1 To ns = s + a(i, j)Next jPrint "第"; i; "行元素之和;"; sNext iPrints = 0For i = 1 To ns = s + a(i, i)Next iPrint "主对角线元素之和;"; sPrints = 0For i = 1 To nFor j = 1 To nIf i = 1 Or i = n Or j = 1 Or j = n Then s = s + a(i, j)End IfNext jNext iPrint "所有靠边元素之和;"; sEnd Subex0605Option Base 1Private Sub Form_Load()Dim a(8, 8) As Integer, n As IntegerDim max As Integer, r As Integer, c As Integer ShowRandomizen = 8For i = 1 To nFor j = 1 To nx = Int(900 * Rnd + 100) '产生一个随机数a(i, j) = xPrint x;Next jPrintNext imax = 0: r = 1: c = 1PrintFor i = 1 To nFor j = 1 To nIf a(i, j) > max Then '判断有无更大数max = a(i, j): r = i: c = jEnd IfNext j, iPrint "最大值为:"; maxPrint "位于行:"; r, "列:"; cEnd Subex0606Dim n As IntegerPrivate Sub Cmd1_Click()n = n + 1If n = 3 Then n = 0Opt1(n).Value = TrueEnd SubPrivate Sub Form_Load()n = 1End Subex0607Private Sub Form_Load()Dim a(20) As IntegerRandomizea(1) = Int(10 + 90 * Rnd) k = 1Do While k < 20x = Int(10 + 90 * Rnd) f = 0For j = 1 To kIf x = a(j) Then f = 1Exit ForEnd IfNext jIf f = 0 Thenk = k + 1a(k) = xEnd IfLoopShowFor j = 1 To 20Print a(j)Next jEnd Subex0701Private Sub Form_Click() Dim s As Longs = 0For k = 1 To 10s = s + jc(k)Next kPrint sEnd SubFunction jc(n) As Long Dim t As Longt = 1For k = 1 To nt = t * kNext kjc = tEnd Functionex0702Private Sub Form_Load()Dim a As Single, b As Single, c As SingleDim d As Single, m As Single, n As SingleShowa = Val(InputBox("输入第1个数"))b = Val(InputBox("输入第2个数"))c = Val(InputBox("输入第3个数"))d = Val(InputBox("输入第4个数"))m = max(a, b) '求a、b中的较大数n = max(c, d) '求c、d中的较大数Print "最大数为:" & max(m, n) '求m、n中的较大数End SubFunction max(x, y) As SingleIf x > y Thenmax = xElsemax = yEnd IfEnd Functionex0703Private Sub Command1_Click()Dim d(5) As Single, m As Single For k = 0 To 5d(k) = Val(Text1(k).Text)Next kCall Max(d(0), d(1), d(2), m) Call Max(m, d(3), d(4), m)Call Max(m, m, d(5), m)Text2.Text = mEnd Subex0704Private Sub Form_Load()Dim x As LongShowx = Val(InputBox("输入一个数")) Print x; "的位数为:"; fnws(x) End SubFunction fnws(x As Long) As Integer Dim n As Integern = 1Do While x \ 10 > 0n = n + 1x = x \ 10Loopfnws = nEnd Functionex0705Private Sub C1_Click()Form1.HideEnd Subex0705APrivate Sub C2_Click()Form1.ShowEnd Subex0706Dim n As IntegerPrivate Sub Command1_Click() usern = Trim(Text1.Text)passw = Trim(Text2.Text)If usern = "username" And passw = "password" ThenForm1.HideForm2.ShowElsen = n + 1Text1.SetFocusIf n = 3 ThenForm1.HideForm3.ShowEnd IfEnd IfText1.Text = ""Text2.Text = ""End Subex0706APrivate Sub Command1_Click()Form2.HideForm1.ShowEnd Subex0906BPrivate Sub Command1_Click()Unload Form1Unload Form2Unload Form3EndEnd Subex0802Private Sub Form_Load()Dim i As Integer, j As IntegerShowFor i = 1 To 12Print String(20 - i, 32); '输出当前行左边的空格,分号表示下一输出项紧接输出For j = 1 To 2 * i - 1 '输出2*i-1个字符If j <= i Then '按左、右两边指定输出字符k = j '左边输出的字符k = 2 * i - j '右边输出的字符End IfIf k > 9 Then k = k + 7 '字母的ASCII 码比数字大7Print Chr(k + 48); '以字符格式输出Next jPrint '换行Next iEnd Subex0803Dim t As Single, f As IntegerPrivate Sub Command1_Click() '“产生原稿文”按钮Randomizes = ""For k = 1 To 30x = Chr(Int(Rnd * 26) + 97) '随机产生小写字母Next kText1.Text = s '显示在文本框中Text2.Text = ""Text2.Locked = False '允许录入Text2.SetFocus '设置焦点Text3.Text = ""Text4.Text = ""f = 0 '第1次按键标记,0表示未按键End SubPrivate Sub Command2_Click() '“结束”按钮EndEnd SubPrivate Sub Text2_KeyUp(KeyCode As Integer, ShiftAs Integer)Dim c As Integer, k As IntegerIf f = 0 Then'第1次按键时,开始计时t = Timer '用t保存第1次按键的时间f = 1 '1表示已按键和进入录入过程End IfIf Len(Text2.Text) < 30 ThenText3.Text = Round(Timer - t, 1) & "秒" '显示用户当前所用时间Elsec = 0For k = 1 To 30 '统计录入正确的字母个数If Mid(Text1.Text, k, 1) =Mid(Text2.Text, k, 1) Thenc = c + 1End IfNext kText2.Locked = True '禁止录入Text4.Text = Round(c / 30 * 100, 2) & "%" '显示准确率End IfEnd Subex0901Private Sub Command1_Click()Open "静夜思.txt" For Input As #1Do While Not EOF(1) '文件未结束时,循环Input #1, xList1.AddItem xLoopClose #1End Subex0902Dim d(1 To 20) As IntegerPrivate Sub Command1_Click() '"产生随机数" For i = 1 To 20d(i) = Int(1 + 99 * Rnd)List1.AddItem d(i)Next iEnd SubPrivate Sub Command2_Click() '"保存" Open "MyFile4.txt" For Output As #1 For i = 1 To 20Write #1, d(i)Next iClose #1End SubPrivate Sub Command3_Click() '"读出" Open "MyFile4.txt" For Input As #1Do While Not EOF(1)Input #1, xList2.AddItem xLoopClose #1End SubPrivate Sub Command4_Click() '"结束" EndEnd Subex0903Private Sub Form_Load()ShowFor k = 1 To 6fn = "..\第7章\ex070" & k & ".vbp"If Dir(fn) = "" ThenPrint Mid(fn, 4) & "未编写!!"ElsePrint Mid(fn, 4) & "已编写!!"End IfNext kEnd Subex0904Private Sub Form_Load()chedir = "C:\Documents and Settings\Lin\My Documents\fsodir"If Dir(chedir, 16) = "" ThenMkDir chedirEnd Ifchefile = "C:\Windows\notepad.exe"If Dir(chefile) <> "" ThenFileCopy chefile, chedir & "\mynotepad.exe"End IfMsgBox "操作完毕!"End Subex1002Private Sub A1_Click()Label1.ForeColor = vbRedEnd SubPrivate Sub A2_Click()Label1.ForeColor = vbBlueEnd SubPrivate Sub A3_Click()Label1.ForeColor = vbGreenEnd SubPrivate Sub Label1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)If Button = 2 ThenPopupMenu mun0End IfEnd Subex1003Private Sub Command1_Click()CommonDialog1.DialogTitle = "选择文件"CommonDialog1.Filter = "文本文件|*.txt"CommonDialog1.InitDir = "C:\"CommonDialog1.ShowOpenText1.Text = CommonDialog1.FileName End SubPrivate Sub Command2_Click()Unload MeEndEnd Subex1101Private Sub calzc(x As Integer)Text2.Text = Round(2 * 3.14159 * x, 2) End SubPrivate Sub HV_Scroll()Dim r As Integerr = HV.ValueText1.Text = "=" & rCall calzc(r)End SubPrivate Sub Form_Load()HV.Max = 300End Subex1102Private Sub Form_Load()For k = 0 To 3Image1(k).Stretch = TrueImage1(k).Picture = LoadPicture("箭头" & k + 1 & ".bmp")Next kEnd SubPrivate Sub Image1_Click(Index As Integer)Picture1.Font.Size = 20Picture1.ClsSelect Case IndexCase 0Picture1.Print "箭头向上"Case 1Picture1.Print "箭头向左"Case 2Picture1.Print "箭头向右"Case 3Picture1.Print "箭头向下"End SelectEnd Sub41。
VB程序设计教程课后答案
1、 简述 VB6.0的主要特点 答:方便、直观的可视化设计工具;面向对象程序设计方法;事件驱动的编程机制;易学易用的应用程 序集成开发环境;结构化程序设计语言;完备的联机帮助功能;强大的多媒体、数据库和网络功能。 2、 当正常安装 VB6.0后,误把 Windows 子目录删除。当重新安装 Windows 后,是否需要再次安装 VB6.0? 答:要重新安装的。因为安装 VB6.0时,有些 VB 程序被系统自动安装在 Windows 目录下,所以一旦删 除 Windows 子目录,就必须重新安装 VB6.0。 3、 Visual Basic 6.0分为学习版、专业版和企业版,如何知道所安装的是哪个版本? 答:在 VB6.0的启动界面上,能显示对应的版本。 4、 叙述建立一个完整应用程序的过程 答:首先建立用户界面的控件对象(简称控件) ;然后对控件属性进行设置;再对控件事件过程及编程; 保存应用程序;程序调试和运行。 5、 Visual Basic 6.0有多种类型窗口,若要在设计时看到代码窗口,应怎样操作? 答:选择“视图”—>“代码窗口”命令或单击“工程资源管理器”窗口中的“查看代码”按钮。 6、 当建立好一个简单的应用程序后, 假定该工程仅有一个窗体模块。 试问该工程涉及多少个要保存的文件? 若要保存该工程的所有文件, 应先保存什么文件?再保存什么文件?若不这样做, 系统会出现什么信息? 答:涉及两个文件要保存。先保存窗体文件(.frm) ,再保存工程文件(.vbp) 。若先保存工程文件,系统 也会先弹出“文件另存为”对话框,要求先保存窗体文件。 7、 保存文件时,若不改变目录名,则系统默认的目录是什么? 答:系统默认的目录是 VB98 8、 安装 Visual Basic 6.0后,帮助系统是否也已安装? 答:没有。VB6.0的帮助系统在两张光盘上,即 MSDN Library,其中还包含了 VC、VFP 等其他语言的帮 助系统。 9、 如何使用 Visual Basic 6.0的帮助系统? 答:最方便的方法是选定要帮助的内容,然后按 F1功能键,这时系统打开 MSDN Library 查阅器,直接 显示与选定内容有关的帮助信息。 10、 什么是类?什么是对象?什么是事件过程? 答:类是同类对象集合的抽象,它规定了这些对象的公共属性和方法; 对象是类的一个实例,对象包含三个要素:属性、方法和事件。 当在对象上发生了事件后,应用程序就要处理这个事件,而处理的步骤就是事件过程。 11、 属性和方法的区别是什么? 答:属性是对象的特征,方法是对象的行为。前者有值,可以被赋值或引用;后者没有值,表示能够执 行的操作。 12、 当标签边框的大小由 Caption 属性的值进行扩展或缩小时,应对该控件的什么属性进行何种设置? 答:将该控件的 Autosize 属性值设置为 true。 13、 在 VB6.0中,命令按钮的显示形式可以有标准的和图形的两种选择,它们通过什么属性来设置?若选 择图形的,则通过什么属性来装入图形?若已在规定的属性里装入了某个图形文件,但该命令按钮还是 不能显示该图形,而显示的是 Caption 属性设置的文字,应怎样修改? 答: 命令按钮的显示形式通过将 Style 属性值设置为 Graphical 来实现。 通过 Picture 属性来装入图形。 不
VB程序设计教程课后参考答案
VB程序设计教程课后参考答案习题一一、填空题1.对象、事件驱动2.窗体、控件3.属性4.属性窗口、编程设置5.事件过程6.对象名、下划线、事件名7.Form_click8.Print form1.print “我的VB”9.主窗口、属性、工具箱、代码、窗体、工程资源管理10.菜单、快捷键二、选择题1.C 2. A 3. B 4. B 5. D 6. B C 7. A 三、简答题(略)习题二一、选择题1. D2. C3. C4. B5. C6. B7. C8. B 9. A 10. B 11. B 12. C 13. D 14. B15. A 16. A 17. A 18. A 19. B 20. C 21. A22. B 23. B 24. A 25. C 26. A 27. B 28. B29. C 30. B 31. D 32. D 33. C二、填空题1.Controlbox、Maxbutton、Minbutton2.Multiline TRUE3.******4.单文档界面(SDI)多文档界面(MDI)5.窗体显示6.Passwordchar7._change()三、编程题(略)习题三一、基础知识题(略)二、填空题1.整型、长整型、单精度、双精度2.(x mod 10)*10+x\103.Sin(3.14*15/180)+sqr(x+exp(3))/abs(x-y)-log(3*x)4.(a+b)/(1/(c+5)-c*d/2)5.X mod 5=0 or x mod 9=06.False7.-4、3、-3、3、-4、48.CDEF9.GBCDEFA10.TRUE11.变量、属性12.‘_ :13.(略)14.“”#15.Public、通用/模块、Dim、static、private16.+ 、&、+17.Now() 、time()、日期18.Inputbox()、字符串、val()19.Msgbox()三、选择题1.B 2. C 3. A 4. B D此题有误 5. B 6. A7. D 8. B 9. B 10. C 11. D 12. B13. C 14. B 15. A 16. C 17.A 18. D19. A 20. D 21. A 22. C 23. C 24. D25. A 26. D 27. D 28. B 29. C 30. B31. C 32. B 33. C D此题有误34. C 35. A36. D 37. C 38. A 39. B 40. A 41. C42. C 43. D 44. C 45. D 46. A四、编程题1.Option ExplicitPrivate Sub Command1_Click()Dim a%, b%, c%, delta%Dim x1#, x2#, x#a = Val(InputBox("please input data to a:", "info", 1))b = Val(InputBox("please input data to b:", "info", 1))c = Val(InputBox("please input data to c:", "info", 1))If a = 0 And b <> 0 Thenx = -c / bPrint "所构成的方程是:" & b & "x" & "+" & c & "=0"Print "该一元一次方程的根是:", xEnd IfIf a = 0 And b = 0 ThenPrint "很抱歉,不能构成合法的方程!"End IfIf a <> 0 Thendelta = b * b - 4 * a * cIf delta >= 0 Thenx1 = (-b + Sqr(delta)) / (2 * a)x2 = (-b - Sqr(delta)) / (2 * a)Print "所构成的方程是:" & a & "x2" & "+" & b & "x" & "+" & c & "=0"Print "该方程的根是:"Print "第一个根是:", x1Print "第二个根是:", x2ElsePrint "所构成的方程是:" & a & " x2" & "+" & b & "x" & "+" & c & "=0"Print "该方程有两个虚根:"Print "第一个虚根是:" & -b / (2 * a) & "+" & Sqr(Abs(delta)) / (2 * a) & "i"Print "第二个虚根是:" & -b / (2 * a) & "-" & Sqr(Abs(delta)) / (2 * a) & "i" End IfEnd IfEnd Sub2.Option ExplicitPrivate Sub Command1_Click()Dim i%, s%s = 0For i = 1 To 99 Step 2s = s + iNextPrint sEnd Sub3.Option ExplicitPrivate Sub Command1_Click()Dim i%Dim s#s = 0For i = 1 To 100s = s + 1 / i * (-1) ^ (i + 1)NextPrint sEnd Sub4.Option ExplicitPrivate Sub Command1_Click()Dim i%, n%Dim t#, s#n = Val(InputBox("please input data:", "info", 5))s = 0t = 1For i = 1 To nt = t * is = s + tNextPrint sEnd Sub5.Option ExplicitPrivate Sub Command1_Click()Dim a%, b%, c%Dim p#, s#a = Val(InputBox("please input data to a:", "info", 3))b = Val(InputBox("please input data to b:", "info", 4))c = Val(InputBox("please input data to c:", "info", 5))。
VB程序设计学习与实验指导书答案(包含实验答案)
VB学习与实验指导测试题参考答案第一章第二章第三章第四章第五章第六章第七章第八章第九章综合测试题一综合测试题二下篇实验教程实验2 Visual Basic 变量、函数与表达式的使用实验2-1 算术运算符的使用13178332262-2-22007-10-153 2.857143 2.85714285714286实验2-2 字符运算符的使用20071020071020172007-10-10102007-10-20出错,类型不匹配实验2-3 关系运算符的使用FalseFalseFalseFalseFalseFalseFalseTrueFalseTrue实验2-4 逻辑运算符的使用TrueTrueFalseTrueFalseFalseTrueTrueFalse5-59实验2-5 常用函数的使用3.14 3.147.389056098930651 -1 .999999998205103 4-1 1 0.70554755825 251250 0A a65 9748 688 -98 -89 -9aaa 4.5 aaa4.516 5Visua 6.0sua Basic Basic 6.0abcde EFGBasic Basic BasicVisual Basic 6.0 Basic8 8aaa dd2011-3-152011-3-15 14:35:5215320113001.731.731.7317.32E-010173%实验3 算法基础及程序控制结构实验3-1 三个数的交换A=CC=BB=TBC实验3-2 求周长和面积Const PI=3.14159262*PI*RPI*R*RPicture1.ClsFormat(L, ".00")Format(M, ".00")实验3-3 求分段函数的值x=Val(Text1.Text)y=1+Sin(x)y=Log(x)Text1.Text = ""Text2.Text = ""实验3-4 求一元二次方程的根Val(Text2.Text)Val(Text3.Text)b ^ 2 - 4 * a * c"x1=" & (-b + Sqr(delt)) / (2 * a)"x2=" & (-b - Sqr(delt)) / (2 * a)Text2.Text = ""Text3.Text = ""End实验3-5 判断素数N Mod k = 0k=NN Mod i=0k=2实验3-6 求最大公约数和最小公倍数M<NL=ML=NM Mod k = 0 And N Mod k = 0Text3.Text = kExit ForM>NL=ML=Nk Mod M = 0 And k Mod N = 0Text4.Text = kExit For实验3-7 判断升序数、降序数CStr(N)Len(S) – 1Mid(S, k, 1) >= Mid(S, k + 1, 1)k = Len(S)N = Val(Text1.Text)S = CStr(N)For k = 1 To Len(S) - 1If Mid(S, k, 1) <= Mid(S, k + 1, 1) Then Exit For Next kk = Len(S)实验3-8 判断回文数1Len(s)\2-1Mid(S, k, 1) <> Mid(S, Len(S) - k + 1, 1)k = Len(S) \ 2Mid(S, k, 1) + S1S = S1实验3-9 求级数的值Val(Text1.Text)1x ^ ns * nt / s <= 10 ^ -6Text2.Text = y实验3-10 查找数字串并求和Text1.Text = ""Text2.Text = ""List1.ClearText1.TextFalseWhile Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Flag = Truek = k * 10 + Mid(s, i, 1)i = i + 1Flag = Truesum + kText2.Text = sum实验3-11 随机产生20个奇数Int((99 - 10 + 1) * Rnd) + 10a Mod 2 = 1n Mod 5 = 0Picture1.Cls实验3-12 加密解密Chr(Asc("a") + (Asc(c) - Asc("a") + 3) Mod 26)c = Chr(Asc("A") + (Asc(c) - Asc("A") + 3) Mod 26) c & s2Text2.TextLen(s2)Mid(s2, i, 1)c = Chr(Asc("a") + (Asc(c) - Asc("a") + 23) Mod 26) c = Chr(Asc("A") + (Asc(c) - Asc("A") + 23) Mod 26) c & s1s1Text1.Text = ""Text2.Text = ""实验3-13 统计单词个数Text1.TextMid(s, i, 1) <> " " And i <= Len(s)i = i + 1Mid(s, start, i - start)nText1.Text = ""List1.Clear实验4 数组的使用实验4-1 一维数组的产生及输出i = 1 To 20Int((9 - 0 + 1) * Rnd + 0)i = 1 To 20Print a(i);Printi = 1 To 20Picture1.Print a(i);If i Mod 5 = 0 Then Picture1.Printi = 1 To 20Text1.Text & Space(2) & a(i)If i Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf v In aIf i Mod 5 = 0 Then Picture2.Print实验4-2 选手得分Int((50 - 10 + 1) * Rnd + 10) / 10i = 1 To 6sum = sum + score(i)max = score(1)min = score(1)score(i) > max Then max = score(i)score(i) < min Then min = score(i)(sum - max - min) / 4实验4-3 产生10个互不相同的整数Int((999 - 10 + 1) * Rnd + 10)t Mod 10 = 5Then Exit Forn= n + 1a(i) = tFor i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintNext i实验4-4 统计数字出现的次数IsNumeric(S) = FalseExit SubLen(S)c >= "0" And c <= "9"A(c) = A(c) + 1A(k) <> 0m = m + 1实验4-5 排序1 To 10Int((99 - 10 + 1) * Rnd + 10)Text1.Text & A(n) & " "1 To 9i + 1 To 10A(i) > A(j)T = A(j)A(j) = A(i)A(i) = T1 To 910 - iA(j) > A(j + 1)T = A(j + 1)A(j + 1) = A(j)A(j) = T1 To 10Text3.Text = Text3.Text & A(i) & " "10T = A(i)ji - 1k + 11 To 10Text4.Text = Text4.Text & A(i) & " "实验4-6 二维数组的产生及输出1 To 41 To 5Int((9 - 0 + 1) * Rnd + 0)1 To 41 To 5Print A(i, j);Print1 To 41 To 5Picture1.Print A(i, j);Picture1.Print1 To 41 To 5Text1.Text = Text1.Text & A(i, j) & " "Text1.Text = Text1.Text & vbCrLf实验4-6 查找最大和最小元素及其位置1 To 31 To 4A(i, j) = Int((99 - 10 + 1) * Rnd + 10)Picture1.Print A(i, j);Picture1.PrintA(1, 1), 1, 1A(1, 1), 1, 1A(i, j)ijA(i, j) < minA(i, j)ij"最大元素" & "A(" & maxi & "," & maxj & ")=" & max "最小元素" & "A(" & mini & "," & minj & ")=" & min实验4-7 矩阵转置Dim A() As Integer"请输入N的值"ReDim A(N, N) As IntegerFor i = 1 To NFor j = 1 To NA(i, j) = Int((99 - 10 + 1) * Rnd + 10)Text1.Text = Text1.Text & A(i, j) & " "Next jText1.Text = Text1.Text & vbCrLfNext it = A(i, j)A(i, j) = A(j, i)A(j, i) = tFor i = 1 To NFor j = 1 To NText2.Text = Text2.Text & A(i, j) & " "Next jText2.Text = Text2.Text & vbCrLfNext i实验5 过程实验5-1 孪生素数和降序素数Dim i As IntegerFor i = 2 To N - 1If N Mod i = 0 Then Exit FunctionNext iPrime = TrueDim a As IntegerDim b As Integera = N \ 10b = N Mod 10If a > b ThenDecNumber = TrueElseDecNumber = FalseEnd If10 To 100Prime(i) = True And Prime(i + 2) = TrueList2.AddItem i & "和" & i + 2DecNumber(i) = True And Prime(i) = TrueList3.AddItem i实验5-2 求多项式和k = 1a = f(x, k)S + ak = k + 1Single As SingleDim i As Integer, a As Singlea = (x + 1) / xFor i = 2 To ka = a * (x + i) / ((2 * i - 1) * x)Next ia实验5-3 进制转换Asc(C) - Asc("A") + 10Left(S, k - 1)Mid(S, k + 1)Change(Mid(S2, i, 1)) * N ^ (0 - i)T1 + T2Tran(8, S)Tran(16, S)Not (C >= "0" And C <= "7" Or C >= "A" And C <= "F" Or C = ".")实验5-4 Armstrong数i As Long, a As Long, b As LongCombo1.Text实验5-5Option Base 1Dim a(5) As IntegerDim b(5) As Integerb(i) = i * i + 1Text2.Text = Text2.Text & b(i) & " "1 To 51 To 5Gcd(a(i), b(j)) = 1Gcd = KExit For实验5-6Create(a)Output(a)Sort(a)Output(a)GetMaxMin(a, MaxData, MinData)MaxDataMinDataAverage(a)Int((99 - 10 + 1) * Rnd + 10)1 To UBound(a)a(i);a(i) > a(j)t = a(j)a(j) = a(i)a(i) = t2 To UBound(a)a(i) > MM = a(i)a(i) < NN = a(i)s = s + a(i)Average = s / UBound(a)实验6 界面设计实验6-1 用户登陆MsgBox "请登录!"PassWordInput=TxtPassW.TextUserIdInput=UserId And PassWordInput=PassWordCmdReset_Click实验6-2 学籍登记OptXb(0).Value = TrueList1.AddItem Space(4) & xh & Space(6) & xm & Space(6) & xb & Space(12) & xy & Space(16) & zy & Space(4)List1.RemoveItem List1.ListIndex实验6-3 字体格式设置Select Case OpFontCheck2.Value = 1OpColor = IndexOpFont = Index实验6-4 兴趣调查List1.AddItem ItemEndSelect Case IndexItem = Text1.Text实验6-5 调色板。
vb程序设计教程课后答案与实验题答案(第三版)
习题一答案在教材上找。
习题二1.对象指现实世界中的实体。
每个对象有自己的特征(属性)、行为(方法)和发生在该对象上的活动(事件)。
类是对同一种对象的集合与抽象。
类包含所创建对象的属性数据,以及对这些数据进行操作的方法。
封装和隐藏是类的重要特性,将数据结构和对数据的操作封装在一起,类的内部实现细节对用户来说是透明的。
VB工具箱中的可视图标是VB系统设计好的标准控件类。
通过将控件类实例化,得到真正的控件对象。
在窗体上画一个控件,就将类转换为对象,创建了一个控件对象。
VB中常用的对象是控件。
此外,VB还提供了系统对象。
窗体是个特例,它既是类也是对象。
当向一个工程添加一个新窗体时,实质就由窗体类创建了一个窗体对象。
对象的三要素:属性、事件和方法。
属性:存放对象的数据(决定对象外观及功能)对象属性的设置方法:在设计阶段,利用属性窗口直接设置属性值;程序运行时,通过语句[对象名.]属性名=属性值。
若属性只能在设计阶段设置,在程序运行阶段不可改变,称为只读属性。
事件:发生在对象上的事情。
同一事件,对不同的对象,会引发不同的反应。
事件过程:应用程序处理事件的步骤。
应用程序设计的主要工作就是为对象编写事件过程的代码。
方法:面向对象的程序设计语言,为程序设计人员提供了一种特殊的过程和函数。
方法是面向对象的,调用时一定要指明对象。
2.窗体和控件具有的基本属性“(1) Name属性:名称所创建的对象名称,在程序中引用。
(2) Caption属性:标题该属性决定了对象上显示的内容。
(3) Height、Width、Top和Left属性决定对象的大小和位置。
(4) Enabled 属性:对象是否允许操作(5) Visible 属性:对象是否可见(6) Font 属性组:文本的外观FontName字体、FontSize大小FontBold粗体、FontItalic斜体FontStrikethru 删除线FontUnderline 下划线(7) ForeColor属性:前景颜色(8) BackColor属性:背景颜色(9) MousePointer属性:鼠标指针类型, 设置值范围0~15,由系统指定。
vb程序设计课后习题答案
vb程序设计课后习题答案VB程序设计课后习题答案在学习VB程序设计课程的过程中,课后习题是巩固知识、提高能力的重要环节。
通过认真完成课后习题,我们不仅可以加深对知识点的理解,还可以提高编程能力和解决问题的能力。
下面我将为大家分享一些VB程序设计课后习题的答案,希望能够对大家的学习有所帮助。
1. 编写一个VB程序,实现输入两个数,计算它们的和并输出。
```Private Sub btnCalculate_Click()Dim num1 As IntegerDim num2 As IntegerDim sum As Integernum1 = Val(txtNum1.Text)num2 = Val(txtNum2.Text)sum = num1 + num2lblResult.Caption = "结果为:" & sumEnd Sub```2. 编写一个VB程序,实现输入一个数,判断它是奇数还是偶数并输出。
Private Sub btnJudge_Click()Dim num As Integernum = Val(txtNum.Text)If num Mod 2 = 0 ThenlblResult.Caption = "这是一个偶数"ElselblResult.Caption = "这是一个奇数"End IfEnd Sub```3. 编写一个VB程序,实现输入一个数,计算它的阶乘并输出。
```Private Sub btnFactorial_Click()Dim num As IntegerDim result As IntegerDim i As Integernum = Val(txtNum.Text)result = 1For i = 1 To numresult = result * iNextlblResult.Caption = "结果为:" & resultEnd Sub```通过以上几个例子,我们可以看到,VB程序设计课后习题涵盖了基本的输入输出、运算符的使用、条件判断和循环等知识点。
大学vb上机及课后习题答案实验1-2
实验1.1.1 标签计算机等级考试True三号1实验1.1.2 200020012等级考试实验1.1.3 “”“”T2=T1实验1.1.4 改变按钮大小C1变宽C1.Width=C1.Width=100实验1.1.5 确认FalseFalseL1请确认C1是300800TrueC2否300800True实验1.1.6 列表框的使用北京山西辽宁浙江广东实验1.1.7 True3True3C1复制Text2=Text1实验1.1.8 计算机等级考试C1移动Text1.Left=0实验1.1.9 程序设计三号2粗体斜体TrueTrue实验1.2.1 计算机等级考试对齐方式字体左对齐居中右对齐宋体黑体Text1.Alignment=2Text1.Alignment=1“宋体”Text1.FontName=”黑体”实验1.2.2 选课CH1数学CH2语文CH3外语CH4计算机1实验1.2.3 20003000FalseCmd1显示图片Cmd2隐藏图片LoadPicture(App.Path&”\图片1.jpg”) LoadPicture(“”)实验1.2.4 FalseC:\VB\Sy02\图片2.jpg设置属性100240020020Picture1.Height=VScroll1.Value实验1.2.5 11001HScroll1.SetFocusText1=HScroll1.Value实验1.2.6 图形控件L11 2 3 4 5Shape1.Shape=L1.Text实验1.2.7 图形控件图形红色边框Shape1.Shape=3&HFF&实验1.2.8True“”三号1000Label1.Caption=Now。
VB程序设计教程实验指导与习题解答上机答案
自测1程序代码Private Sub Command1_Click() = + 3End SubPrivate Sub Command2_Click() = TrueEnd SubPrivate Sub Command3_Click() = TrueEnd SubPrivate Sub Command4_Click() = 10= False= FalseEnd Sub初始窗口:单机放大之后窗口:单机下划线之后窗口:单击加粗之后窗口:单击还原之后窗口:自测2程序代码Private Sub Command1_Click() Label1 = "visual basic"Text1 = "程序设计题"End SubPrivate Sub Command2_Click() Text1 = "visual basic"Label1 = "程序设计题"End Sub初始窗口:单击显示之后窗口:单击交换之后的窗口:自测题3Private Sub Command1_Click() = *End SubPrivate Sub Command2_Click() = + 30= + 20= 1End Sub初始窗口:单击缩小两倍之后的窗口:单击向右下角移动之后的窗口:自测题4程序代码Private Sub Command1_Click() = "VB学习"Print "简单的VB程序设计" End SubPrivate Sub Command2_Click() ClsPrivate Sub Command3_Click() EndEnd SubPrivate Sub Command3_Click() EndEnd Sub初始化窗口:单击显示之后的窗口:单击清除之后的窗口:单击退出之后的窗口:程序代码Private Sub Command1_Click() = ""= ""End SubPrivate Sub Command2_Click() = Val + ValEnd SubPrivate Sub Command3_Click() EndEnd Sub初始化窗口:单击运算之后窗口:单击清除之后的窗口:单击退出之后的窗口:自测练习1Private Sub Command1_Click() Dim a$a == Left(a, 2)End SubPrivate Sub Command2_Click() a == Right(a, 2)End SubPrivate Sub Command3_Click() = &End Sub初始窗口界面输入字符串截取前两个字符截取后两个字符字符串连接自测练习2Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f%, g%, h%, i%, j%, k%, l% a = 7349e = a Mod 10 '个位d = (a \ 10) Mod 10 '十位c = (a \ 100) Mod 10 '百位b = a \ 1000 '千位i = (e + 7) Mod 10 '个位h = (d + 7) Mod 10 '十位g = (c + 7) Mod 10 '百位f = (b + 7) Mod 10 '千位j = ii = f '交换后的个位f = j '交换后的千位k = hh = g '交换后的十位g = k '交换后的百位l = f * 1000 + g * 100 + h * 10 + i= FalsePrint "加密前的4位整数是7439"; (Chr(10)); "加密后的结果是"; lEnd Sub初始界面单击窗体运行后的界面自测练习1Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f!a = Val(InputBox("输入第一个数", "输入一个数", 8))b = Val(InputBox("输入第一个数", "输入一个数", 7))c = a + bd = a - be = a * bf = a / bf = Fix(f * 100 + / 100MsgBox "x+y=" & c & Chr(10) & "x-y=" & d & Chr(10) & "x*y=" & e & Chr(10) & "x/y=" & f, , "输出的结果"End Sub自测练习2Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f!a = Val(InputBox("输入第一个数的值"))b = Val(InputBox("输入第二个数的值"))c = Val(InputBox("输入第三个数的值"))d = Val(InputBox("输入第四个数的值"))e = a + b + c + df = e / 4Print "和="; ePrint "平均值="; fEnd Sub自测练习3Private Sub Form_Click()Dim h%, f%, x%, y%h = Val(InputBox("输入总头数"))f = Val(InputBox("输入总脚数"))x = (4 * h - f) / 2y = (f - 2 * h) / 2Print "笼中鸡的头数为"; x; "只"Print "笼中兔的头数为"; y; "只"End Sub自测练习1:Private Sub Form_Click()Dim a%, b%, c%a = Val(InputBox("请输入第一条边", "边长"))b = Val(InputBox("请输入第二条边", "边长"))c = Val(InputBox("请输入第三条边", "边长"))If a + b > c And a + c > b And b + c > a ThenIf a * a + b * b = c * c Or a * a + c * c = b * b Or b * b =c * c = a * a ThenPrint "直角"ElseIf a = b And b = c ThenPrint "等边三角形"ElseIf a = b Or a = c Or b = c Then Print "等腰三角形"ElsePrint "一般三角形"End IfElsePrint "不是三角形"End IfEnd Sub不是三角形运行结果:初始窗口:输入三条边:运行结果:直角三角形运行结果:等边三角形运行结果如下:等腰三角形:一般三角形自测练习2:Private Sub Form_Click()Dim a%, b!a = Val(InputBox("请输入"))If a Mod 2 = 0 And a Mod 3 = 0 And a Mod 5 = 0 Then b = Sqr(a)Print a, bElseEndEnd IfEnd Sub初始化窗口:输入一个数:运行结果:自测练习3Private Sub Command1_Click()Dim a%, b%a = ValSelect Case a > 0Case a < 20MsgBox "行李费为元/kg", , "行李费"Case a >= 20 And a < 50MsgBox "行李费为元/kg", , "行李费"Case a >= 50 And a < 100MsgBox "行李费为元/kg", , "行李费"Case Elseb = * 100 + (a - 100) * : MsgBox "行李费为" & b & "元", , "行李费"End SelectEnd Sub初始窗口:输入10:输入25:输入55:输入120:自测练习1Option Base 1Private Sub Form_Click()Dim i%, a(1 To 20) As Integer a(1) = 1: a(2) = 1Print a(1);Print a(2);For i = 3 To 10a(i) = a(i - 1) + a(i - 2) Print a(i);Next iPrintFor i = 11 To 16a(i) = a(i - 1) + a(i - 2) Print a(i);Next iPrintFor i = 17 To 20a(i) = a(i - 1) + a(i - 2) Print a(i);Next i初始界面:运行结果界面:自测练习2Option Base 1Private Sub Form_Load()= 10000= 5000End SubPrivate Sub Command1_Click()Dim i%, j%, t%, a(1 To 4, 1 To 4) As Integer For i = 1 To 4For j = 1 To 4a(i, j) = Int(Rnd * 10) + 1a(i, j);Next j"对角线的元素如下" For i = 1 To 4a(i, i);Next iFor i = 1 To 4a(i, 5 - i);Next iFor i = 1 To 4t = a(1, i)a(1, i) = a(4, i) a(4, i) = tNext iFor i = 1 To 4t = a(i, 2)a(i, 2) = a(i, 3) a(i, 3) = tNext iFor i = 1 To 4For j = 1 To 4a(i, j);Next iEnd Sub运行初始界面:运运行结果界面:自测练习1Sub itostr(a As Integer)Dim b%, c$a = ValWhile a >= 1c = Chr(a Mod 10 + 48) & ca = a \ 10Wend= c End SubPrivate Sub Command1_Click()Dim a As Integera = ValCall itostr(a)End Sub运行初始界面:运行结果界面:验证其他数字:自测练习2:Public Function area(n) As Double Dim c#, d#, e#, s#n = Valc = (1 - 0) / nWhile e <= 1d = I * c:e = (I + 1) * cs = s + (Sin(d) + Sin(e)) * c / 2 I = I + 1Wendarea = sEnd FunctionPrivate Sub Command1_Click()Dim s As DoubleCall area(n)= area(n)End Sub运行初始界面:运行结果:自测练习3Private Sub Form_click()n = Val(InputBox("请输入n的值")) x = Val(InputBox("请输入x的值")) Print "e的值="; es(n, x)End SubPrivate Function es(n, x)Dim a As Double, b As Double, c As Double, i As Integer a = 1: b = 1: c = 1For i = 1 To na = x * a:b = b * i:c = c + a / bNext ies = cEnd Function运行初始界面:输入数据:运行结果:自测练习4:For i = 1 To 4For j = i + 1 To 5If a(i) > a(j) Thent = a(i): a(i) = a(j): a(j) = tEnd IfNext jNext iFor i = 1 To 5Print a(i); " ";Next iFor i = 1 To 5If Y < a(i) Thent = a(i): a(i) = Y: Y = tEnd IfNext iFor i = 1 To 6a(6) = tNext iEnd FunctionPrivate Sub Command1_Click()Dim i#, a%(1 To 6), Y%Y = Val(InputBox("输入y的值", "输入数据")) Insertfun a%(), YPrintFor i = 1 To 6Print a(i); " ";Next iEnd Sub运行初始界面:插入数据:运行结果:蓝志鹏资源1102 实验8自测练习1:Private Sub Command1_Click() If = 1 Then= "报名参加羽毛球比赛"End IfIf = 1 Then= "报名参加乒乓球比赛"End IfIf = 1 And = 1 Then= "报名参加羽毛球,乒乓球比赛" End IfEnd Sub运行初始界面:运行结果:自测练习4:Private Sub Form_Load()= 2= 24= "黑体"= &HFF&End SubPrivate Sub Option1_Click()= "我是学生"End SubPrivate Sub Option2_Click() = "我是教师"End SubPrivate Sub Option3_Click() = "我是工人"End SubPrivate Sub Option4_Click() = "我是农民"End Sub运行初始界面:运行结果:自测练习6:Private Sub Command1_Click() For i = 0 To - 1If (i) = ThenExit ForEnd IfNext iIf i > - 1 ThenEnd IfEnd SubPrivate Sub Command2_Click() If <> -1 ThenFor i = 0 To - 1If (i) = TheniEnd IfExit ForNext iEnd IfEnd SubPrivate Sub Command3_Click()End Sub运行初始界面:自测练习7:Private Sub Command1_Click() = 1= TrueEnd SubPrivate Sub Form_Load()= 20= 20= 20= 20End SubPrivate Sub Timer1_Timer()= + 10lbltem =If VSbwd >= 100 Then lbltishi = "水开了"= FalseEnd IfEnd Sub运行初始界面:运行结果:自测练习1:Private Sub myhelp_Click()End SubPrivate Sub mymenu1_Click() = "百货大楼"End SubPrivate Sub mymenu2_Click() = "利客隆"End SubPrivate Sub mymenu3_Click() = "稻香村"End Sub运行初始界面:运行结果界面:自测练习2:Private Sub Dis_Click()= "菜单程序设计"= 20= "黑体"End SubPrivate Sub Hid_Click()= FalseEnd Sub运行初始界面:运行结果界面:自测练习3:Private Sub vbhelp_Click()r = MsgBox("帮助", , "程序设计题") End SubPrivate Sub Vbnew_Click()r = MsgBox("新建", , "程序设计题") End SubPrivate Sub vbopen_Click()r = MsgBox("打开", , "程序设计题") End SubPrivate Sub vbsave_Click()r = MsgBox("存盘", , "程序设计题") End Sub运行初始界面:运行结果:自测练习4:Private Sub Command1_Click() = 1End Sub运行初始界面:运行结果:自测练习1:Private Sub Command1_Click() Dim a As String, n%, i%, j% Open "E:\" For Input As #1 Input #1, an = Len(a)j = 0For i = 1 To nIf Mid(a, i, 1) = "%" Then j = j + 1 Next iClose #1Open "E:\" For Output As #1Print #1, jClose #1End Sub运行初始界面:运行结果:自测练习3:Private Sub Form_Click()Dim x$Open "e:\" For Input As #1Do While Not EOF(1)Line Input #1, x$LoopClose #1Open "e:\" For Append As #1Print #1, x$Close #1End Sub运行初始界面:自测练习4Private Sub Form_load()Print " 26个英文字母"PrintPrint " a b c d e f g h i j k l m n o p q r s t u v w x y z"Print "字母个数分别为"End SubPrivate Sub Form_Click()Open "E:\" For Output As #1文本$ = InputBox("请输入文本")n = UCase(文本$)Print #1, nClose #1Dim a%()Open "E:\" For Output As #1 For i = 1 To Len(n)ReDim Preserve a(1 To 26)j = Asc(Mid(n, i, 1)) - 64a(j) = a(j) + 1Next iClose #1For j = LBound(a) To UBound(a) Print a(j);Next jEnd Sub。
vb程序设计教程课后答案与实验题答案-(第三版)剖析
答案习题一答案在教材上找。
习题二1.对象指现实世界中的实体。
每个对象有自己的特征(属性)、行为(方法)和发生在该对象上的活动(事件)。
类是对同一种对象的集合与抽象。
类包含所创建对象的属性数据,以及对这些数据进行操作的方法。
封装和隐藏是类的重要特性,将数据结构和对数据的操作封装在一起,类的内部实现细节对用户来说是透明的。
VB工具箱中的可视图标是VB系统设计好的标准控件类。
通过将控件类实例化,得到真正的控件对象。
在窗体上画一个控件,就将类转换为对象,创建了一个控件对象。
VB中常用的对象是控件。
此外,VB还提供了系统对象。
窗体是个特例,它既是类也是对象。
当向一个工程添加一个新窗体时,实质就由窗体类创建了一个窗体对象。
对象的三要素:属性、事件和方法。
属性:存放对象的数据(决定对象外观及功能)对象属性的设置方法:在设计阶段,利用属性窗口直接设置属性值;程序运行时,通过语句[对象名.]属性名=属性值。
若属性只能在设计阶段设置,在程序运行阶段不可改变,称为只读属性。
事件:发生在对象上的事情。
同一事件,对不同的对象,会引发不同的反应。
事件过程:应用程序处理事件的步骤。
应用程序设计的主要工作就是为对象编写事件过程的代码。
方法:面向对象的程序设计语言,为程序设计人员提供了一种特殊的过程和函数。
方法是面向对象的,调用时一定要指明对象。
2.窗体和控件具有的基本属性“(1) Name属性:名称所创建的对象名称,在程序中引用。
(2) Caption属性:标题该属性决定了对象上显示的内容。
(3) Height、Width、Top和Left属性决定对象的大小和位置。
(4) Enabled 属性:对象是否允许操作(5) Visible 属性:对象是否可见(6) Font 属性组:文本的外观FontName字体、FontSize大小FontBold粗体、FontItalic斜体FontStrikethru 删除线FontUnderline 下划线(7) ForeColor属性:前景颜色(8) BackColor属性:背景颜色(9) MousePointer属性:鼠标指针类型, 设置值范围0~15,由系统指定。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
自测 1 程序代码Private Sub Command1_Click()Label1.FontSize = Label1.FontSize + 3End SubPrivate Sub Command2_Click()单机下划线之后窗口:单击加粗之后窗口:单击还原之后窗口:自测2程序代码Private Sub Command1_Click() Label1 = "visual basic"Text1 = "程序设计题"End SubPrivate Sub Command2_Click() Text1 = "visual basic"Label1.Alignment = 1End Sub初始窗口:单击缩小两倍之后的窗口:单击向右下角移动之后的窗口:自测题4程序代码Private Sub Command1_Click() Form1.Caption = "VB学习" Print "简单的VB程序设计" End SubClsEndEnd单击退出之后的窗口:自测题5程序代码Private Sub Command1_Click() Text1.Text = ""Text2.Text = ""End SubPrivate Sub Command2_Click()Text3.Text = Val(Text1.Text) + Val(Text2.Text)End SubPrivate Sub Command3_Click()EndEnd Sub初始化窗口:单击运算之后窗口:单击清除之后的窗口:单击退出之后的窗口:自测练习1Private Sub Command1_Click()Dim a$a = Text1.TextLabel1.Caption = Left(a, 2) End SubPrivate Sub Command2_Click() a = Text1.TextLabel2.Caption = Right(a, 2) End Sube = a Mod 10 '个位d = (a \ 10) Mod 10 '十位c = (a \ 100) Mod 10 '百位b = a \ 1000 '千位i = (e + 7) Mod 10 '个位h = (d + 7) Mod 10 '十位g = (c + 7) Mod 10 '百位f = (b + 7) Mod 10 '千位j = ii = f '交换后的个位f = j '交换后的千位a = Val(InputBox("输入第一个数", "输入一个数", 8))b = Val(InputBox("输入第一个数", "输入一个数", 7))c = a + bd = a - be = a * bf = a / bf = Fix(f * 100 + 0.5) / 100MsgBox "x+y=" & c & Chr(10) & "x-y=" & d & Chr(10) & "x*y=" & e & Chr(10) & "x/y=" & f, , "输出的结果"End Sub自测练习2Private Sub Form_Click()Dim h%, f%, x%, y%h = Val(InputBox("输入总头数"))f = Val(InputBox("输入总脚数"))x = (4 * h - f) / 2y = (f - 2 * h) / 2Print "笼中鸡的头数为"; x; "只" Print "笼中兔的头数为"; y; "只" End Sub自测练习1:Private Sub Form_Click()ThenElsePrint "一般三角形"End IfElsePrint "不是三角形"End IfEnd Sub不是三角形运行结果:初始窗口:输入三条边:运行结果:ElseEndEnd IfEnd Sub初始化窗口:输入一个数:运行结果:自测练习3Private Sub Command1_Click() Dim a%, b%a = Val(Text1.Text)Select Case a > 0输入25:输入55:输入120:自测练习1Option Base 1Dim i%, a(1 To 20) As Integer a(1) = 1: a(2) = 1Print a(1);Print a(2);For i = 3 To 10Next i初始界面:运行结果界面:自测练习2Option Base 1Form1.Width = 10000Form1.Height = 5000End SubPrivate Sub Command1_Click()Dim i%, j%, t%, a(1 To 4, 1 To 4) As IntegerFor i = 1 To 4Picture3.Print a(i, 5 - i);Next iFor i = 1 To 4t = a(1, i)a(1, i) = a(4, i)a(4, i) = tNext iFor i = 1 To 4t = a(i, 2)a(i, 2) = a(i, 3)a(i, 3) = tNext iFor i = 1 To 4For j = 1 To 4Picture2.Print a(i, j); Next jPicture2.PrintNext iEnd Sub运行初始界面:运运行结果界面:自测练习1Sub itostr(a As Integer)Dim b%, c$a = Val(Text1.Text)While a >= 1Wend验证其他数字:自测练习2:Public Function area(n) As Double Dim c#, d#, e#, s#n = Val(Text2.Text)c = (1 - 0) / nWhile e <= 1d = I * c:e = (I + 1) * cs = s + (Sin(d) + Sin(e)) * c / 2I = I + 1Wendx = Val(InputBox("请输入x的值"))Print "e的值="; es(n, x)End SubPrivate Function es(n, x)Dim a As Double, b As Double, c As Double, i As Integera = 1:b = 1:c = 1For i = 1 To na = x * a:b = b * i:c = c + a / b Next ies = cEnd FunctionPrint a(i); " ";Next iFor i = 1 To 5If Y < a(i) Thent = a(i): a(i) = Y: Y = tEnd IfNext iFor i = 1 To 6a(6) = tNext iEnd Function自测练习1:Private Sub Command1_Click()If Ch1.Value = 1 ThenText1.Text = "报名参加羽毛球比赛" End IfIf Ch2.Value = 1 ThenText1.Text = "报名参加乒乓球比赛"End IfIf Ch1.Value = 1 And Ch2.Value = 1 Then Text1.Text = "报名参加羽毛球,乒乓球比赛" End IfEnd SubPrivate Sub Option2_Click()Label1.Caption = "我是教师"End SubPrivate Sub Option3_Click()Label1.Caption = "我是工人"End SubPrivate Sub Option4_Click()Label1.Caption = "我是农民"End Sub运行初始界面:Private Sub Command2_Click()If Combo1.ListIndex <> -1 Then Combo1.RemoveItem Combo1.ListIndex For i = 0 To Combo1.ListCount - 1If Combo1.List(i) = Combo1.Text ThenCombo1.RemoveItem iEnd IfExit ForNext iEnd IfEnd SubLabel1.FontSize = 20Label2.FontSize = 20End SubPrivate Sub Timer1_Timer() VSbwd.Value = VSbwd.Value + 10lbltem = VSbwd.ValueIf VSbwd >= 100 Thenlbltishi = "水开了"Timer1.Enabled = FalseEnd IfEnd SubPrivate Sub mymenu3_Click() Text1.Text = "稻香村"End Sub运行初始界面:运行结果界面:自测练习2:Private Sub Dis_Click()Text1.Text = "菜单程序设计"Text1.FontSize = 20Text1.FontName = "黑体"End SubPrivate Sub vbopen_Click()r = MsgBox("打开", , "程序设计题") End SubPrivate Sub vbsave_Click()r = MsgBox("存盘", , "程序设计题")End Sub运行初始界面:运行结果:自测练习4:Private Sub Command1_Click() CommonDialog1.Action = 1List1.AddItem CommonDialog1.FileName End Sub运行初始界面:运行结果:自测练习1:Private Sub Command1_Click()Dim a As String, n%, i%, j%Open "E:\Data.txt" For Input As #1Input #1, an = Len(a)j = 0For i = 1 To nIf Mid(a, i, 1) = "%" Then j = j + 1 Next iLine Input #1, x$LoopClose #1Open "e:\txt1.txt" For Append As #1 Print #1, x$Close #1End Sub运行初始界面:自测练习4Private Sub Form_load()Print " 26个英文字母"文本Open "E:\Data.txt" For Output As #1 For i = 1 To Len(n)ReDim Preserve a(1 To 26)j = Asc(Mid(n, i, 1)) - 64a(j) = a(j) + 1Next iClose #1For j = LBound(a) To UBound(a) Print a(j);Next jEnd Sub。