《VB课后习题答案汇总.pdf》由会员分享,可在线阅读,更多相关《VB课后习题答案汇总.pdf(26页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、课后习题参考答案第 1 章 V i s u a l Ba s i c 6.0一、判断题1 5:V X V X V 6 1 0:X X J X J 1 1 1 3:V X X二、选择题1 5:A DBBB 6 1 0:.CA CDB三、填空题1、图片框和框架2、对象3、控 件 屏 幕4、Lo a d5、Lo a d A c t i v a t e四、程序设计题1、Pr i v a t e Su b C o m m a n d l _ Cl i c k()Pr i n t T e x t 1.T e x tEn d Su bPr i v a t e Su b Co m m a n d 2 _ Cl
2、 i c k()En dEn d Su b2、Pr i v a t e Su b C o m m a n d 1 _ Cl i c k()Fo r m 1.Fo n t Si ze =Fo r m 1.Fo n t Si ze +3Pr i n t 青春无悔”En d Su bPr i v a t e Su b Co m m a n d 2 _ Cl i c k()Fo n t Si ze =Fo n t Si ze -3Pr i n t 青春无悔”En d Su bPr i v a t e Su b Co m m a n d 3 _ Cl i c k()Fo r m l.Fo n t Bo
3、l d =T r u ePr i n t 青春无悔”En d Su bPr i v a t e Su b Co m m a n d 4 _ Cl i c k()Fo n t Bo l d =Fa l s ePr i n t 青春无悔”En d Su b3、Pr i v a t e Su b C o m m a n d l _ Cl i c k()Text 1.Visible=FalseEnd SubPrivate Sub Command2_Click()Text 1.Visible=TrueTextl.FontName 二 楷体_gb2312”Text 1.FontSize=15Textl.T
4、ext=我学会了创建个应用程序了!”End Sub第二章判断题:1 5错 错 错 错 错第 3 章顺序结构程序设计一、思 考 题 略二、程序阅读题1.A=2 B=1 C=22.123.BABCBACBC4.1 2 False5.1-12-123-1234-三、选择题LB 2,D 3.B 4.B 5.D 6.C四、程序设计题1.Private sub form_click()Dim r!,s!R=val(inputbox(02.(1)max(2)min(3)cmax(4)cmin3.(1)x mod 3=2 and x mod 5=3 and x mod 7=4 then(2)text 1.se
5、tfocus(3)textl.selstrat=O(4)text 1 .sellength=len(text 1 .text)五、1.Private Sub Command I _Click()Dim y As Integery=Val(InputBox(ny=)If(y Mod 4=0 And y Mod 100 0)Or(y Mod 400=0)ThenPrint”是闰年”ElsePrint”不是闰年”End IfEnd SubPrivate Sub Command 1 _Click()Dim y As Integer,y 1 as stringy=Val(InputBox(“y=)If(
6、y Mod 4=0 And y Mod 100 0)Or(y Mod 400=0)ThenY1二”是闰年”ElseY1二”不是闰年”End If?y,yiEnd SubPrivate Sub Commandl_Click()Dim y As Integer,yl as stringy=Val(InputBox(ny=H)Yl二 不是闰年”If(y Mod 4=0 And y Mod 100 0)Or(y Mod 400=0)ThenYl二”是闰年”End If?y,yiEnd SubPrivate Sub Command l_Click()Dim a%,b%c%,x!,s!a=Val(Inpu
7、tBox(na=H)b=Val(InputBox(nb=)c=Val(InputBox(nc=n)If a+b c And a+c b And b+c a Thenx=(a+b+c)/2s=Sqr(x*(x-a)*(x-b)*(x-c)Print s=H;sElsePrim”不是三角形,重 输a,b,cHEnd IfEnd SubPrivate Sub Command 1 _Click()Dim a%,b%,c%,x!,s!,t%a=Val(InputBox(Ha=u)b=Val(InputBox(ub=n)c=Val(InputBox(c=)4a,b,c=3,4,5t=0Il a+b c A
8、nd a+c b And b+c a Thenx=(a+b+c)/2s=Sqr(x*(x-a)*(x-b)*(x-c)t=lEnd IfIf t=0 then?”不 是“endifEnd SubPrivate Sub Command l_Click()Dim x!,y!x=Val(InputBox(,x=n)y=x*x-x-xIf x 0 And x -3 Theny=x*x+x-6Elself 0=x And x 10 And x 2 And x o 3 Theny=x*x-5*x+6End IfPrint x=;x,y=n;yEnd SubPrivate Sub Command l_Cli
9、ck()Dim x!,y!x=Val(InputBox(Hx=n)If x 0 And x -3 Theny=x*x+x-6elself 0=x And x 10 And x o 2 And x 3 Theny=x*x-5*x+6elsey=xA2-x-lEnd IfPrint x=;x,ny=n;yEnd SubPrivate sub form_click()Dim y%,z%,dj%,p!,t!Y=val(inputbox(“月”)Z=val(inputbox(“订票数”)Dj=val(inputbox(“票价”)Select case ycase 7,8,9If z=20 then p=
10、0.15 else p=0.05case 1 to 5,10,11If z20 then p=0.3 else p=0.2Case elseP=0.2End selectT=z*dj*p?tend sub5.private sub form_click()dim m 1%,m2%,m3%,dj$m 1 =val(inputbox(ml=n):m2=val(inputbox(m2=):m3=val(inputbox(um3=,)if(m 1 +m2+m3)/3=95 or(a=100 and b=100 and c=80)or(a=100 and c=100 and b=80)or(c=100a
11、nd b=100 and a=80)thendj二 一等”elseif(m 1 +m2+m3)/3=90 or(a=100 and b=75 and c=75)or(b=100 and a=75 and c=75)or(c=100 and b=75 and a=75)thendj二 二等”elseif a=75 and b=75 and c=75 thendj二 三等”else可 二,没有,endif?“dj,djend sub第 5 章 循环结构程序设计1.判断题X V V V X X V X2.选择题BBBBCC3.填空题(1)7(2)Do/Loop For/Next(3)-364.程序阅
12、读题(1)88688646886424688642While/Wend 1 2 3 4Sum=10(3)4#5.程序填空题(1)sign=l For i=2 to 19(2)Len(strl)length-1 Mid(strl,i,2)I mod 7=5 and i Mod 5=3 And i Mod 3=2(4)last_one=last_two(2)last_two=this_one(5)Iint(sqr(m)Int(Sqr(n-m)6.编程题(1)Private Sub Form_Click()Dim i As Integer,j As IntegerDim s As Double,Su
13、m As Doubles=1(4)Sum=0 i1000i=i+l i-2Sum=0For i=1 To 11 Step 2Forj=ITois=s*jNext jSum=Sum+sNext iPrint SumEnd Sub(2)Private Sub Form_Click()Dim m%,n%,mn%,r%t%m=Val(InputBox(m=u)n=Val(InputBox(nn=n)If n=0 Or m=0 ThenMsgBox”数据有误”EndEnd Ifmn=m*nIf m=0.00001i=i+l:a=a*x/i:y =y+aWendPrint uy=u;yEnd Sub第六章
14、答案一、判断题1.错2.错3.错4.错(可 以为小数,不过仍然是当作整数来处理)5.错6.错二、选择题1.C2.C3.A4.D三、程序填空1.(1)N4(2)T=A(J):A(J)=A(J+1):A(J+A=T(3)Print A 2.(1)a(i,i)=1 (2)Print四、程序阅读1.52.1 2 3 41 2 3 41 2 3 41 2 3 41 1 1 12 2 2 23 3 3 34 4 4 43.(前面空4 个空格)11211102131693141584567第 7 章过程判断题:1、对2、错3、错4、错5、对6、对7、错选择题:1.B 2、D 3、D 4、C 5、A程序阅读题
15、:1、y=5 a=52、n=1 z=4y=15 a=10n=2 z=7y=30 a=15 n=3z=103、32 644、2 2216648644645、Al=10Bl=20A2=20B2=10程序填空题:1 (1)hwstring(str)(2),n(3)As Boolean(4)n=len(strtxt)(5)True(6)False2.(1)Xsch(Nman,Nkcen)(2)n(3)sum=sum+x(i,j)(4)tt/n(5)x(i,m+1 )v 1.2*ven and x(i,m+1 )=LI*ver3.(1)number(i)(2)Byval n as Inlcgcr(3)n
16、umber=number+7*10八(i-1)4.(1)temp=l(2)4mp(3)nFactoK i)(4)sum程序设计题:1、具体程序为:Function Maxnum(ByRef a()As Integer,ByVai n As Integer)As IntegerDim i As IntegerMaxnum=a(l)For i=2 To nIf a(i)Maxnum Then Maxnum=a(i)Next iEnd FunctionFunction Avenu(ByRef a()As Integer,By Vai n As Integer)As SingleDim i As In
17、teger,sum As IntegerFor i=1 To nsum=sum+a(i)Next iAvenu=sum/nEnd FunctionSub Sortrnum(ByRef a()As Integer,ByVai n As Integer)Dim i As Integer,j As Integer,k As IntegerDim temp As IntegerFor i=1 To nk=iFor j=i+1 To nIf a(k)a(j)Then k=jNext jIf i o k Thentemp=a(i)a(i)=a(k)a(k)=tempEnd IfNext iEnd SubP
18、rivate Sub Form_Click()Dim b(10)As Integer,i As IntegerFor i=1 To 10b(i)=InputBox(”请输入整数”,“输入”)Next iCall Sortrnum(b(),10)Print”从小到大排序为:”For i=1 To 10Print b(i);NextPrintPrint最大数为:”;Maxnum(b(),10)Print平均数为:;Avenu(b(),10)End Sub2、具体程序为:Function fe(n%,x!)As SingleDim temp As Single,i As Integerfe=1tem
19、p=1For i=1 To ntemp=temp*x/ife=fe+tempNext iEnd FunctionPrivate Sub Form_Click()Dim n As Integer,x As Singlen=Val(InputBox(“请输入 n”,“输入”)x=Val(InputBox(请输入 输入”)Print”e 的值为:H;fe(n,x)End Sub3、具体程序为:Sub sort(a()As Single,n As Integer)Dim i As Integer,j As Integer,k As IntegerDim temp As SingleFor i=1 To
20、 nk=iForj=i+1 To nIf a(k)Autosize Wordwrap 2、Enabled Visible 3、文本框和列表框4、拖动滚动框 5、Change 6 Command 1 .Setfocus 7、Timer8、MaxLength 9 Addltem 10、定时器屏蔽四、程序阅读题1、w=4w=13w=135w=312、4637893、李r苹果橘子葡萄柚子香焦4、23451345125.6152845五、程序填空题1、Label 1.left(2)-Label 1.Width2、(1)Listl.ListIndex(1)Timerl.Enabled=True(2)x 3
21、600(3)(x-h*3600)60(4)x=x+16、(1)mlen(strl)/2(2)mid(strl,m+1,1)(3)mid(strl,len(str六、程序设计题1、P rivate Sub Commandl_Click()If T extl.T ext=nn ThenMsgBox 请先输入添加项ElseListl.A dditem T ex tl.TextEnd IfEnd SubP rivate Sub Command2_Click()If L istl.L istlndex 0 Then 或者 if l i s t l.listin d e x=-l thenMsgBox 请
22、选择一删除项ElseL is tl.Removeitem L is tl.ListIndexEnd IfEnd SubP rivate Sub Command3_Click()If L istl.L istin d ex Dim a As IntegerPrivate Sub Commandl_Click()a=Int(Rnd*100)+1End SubPrivate Sub Textl_KeyPress(KeyAscii As Integer)Static i As IntegerIf KeyAscii=13 ThenIf Vai(Text1.Text)a ThenLabell.Captio
23、n=”太大了,请重输”i=i+1Elself Vai(Textl.Text)=a ThenLabell.Caption=恭喜你,你答对了!”i=0Commandl_ClickElseLabell.Caption=太小了,请重输!”i=i+1End IfIf i 10 ThenMsgBox”你失败了!”EndEnd IfEnd IfEnd Sub3、Private Sub Commandl_Click()Dim n As Integer:n=Len(Textl.Text)Dim s(1 To 1000)As String,i As Integer,j As Integer,t As String
24、,a As StringFor i=1 To ns(i)=Mid(Trim(Text1.Text),i,1)Next iFor i=1 To n-1For j=i+1 To nIf s(i)s(j)Thent=s(i):s(i)=s(j):s(j)=tEnd IfNext j,iFor i=1 To n:a=a+s(i):Next iPrint aCommandl.Enabled=False:Command2.Enabled=TrueEnd SubPrivate Sub Command2_Click():End:End SubPrivate Sub Form_Load()Textl.MaxLe
25、ngth=200Command2.Enabled=False:Commandl.Enabled=TrueEnd SubDim a(1 To 1000)As String,b(1 To 1000)As String,c(1 To 1000)As Double,d(1 To 1000)As DoublePrivate Sub Combol_click()Textl.Text=a(Combol.Listindex+1)Text2.Text=b(Combol.Listindex+1)Text3.Text=c(Combol.Listindex+1)Text4.Text=d(Combol.Listlnde
26、x+1)End SubPrivate Sub Commandl_Click()Combol.Additem Textl.Texta(Combol.ListCount)=Textl.Textb(Combol.ListCount)=Text2.Textc(Combol.ListCount)=Vai(Text3.Text)d(Combol.ListCount)=Vai(Text4.Text)End SubPrivate Sub Command2_Click()For i=Combol.ListIndex+1 To Combol.ListCount-1a(i)=a(i+1)b(i)=b(i+1)c(i
27、)=c(i+1)d(i)=d(i+1)Next ia(i)=nnb(i)=n nc(i)=0d(i)=0Combol.Removeitem Combol.ListIndexTextl.Text=Text2.Text=Text3.Text=Text4.Text=End SubPrivate Sub Form_Load()Dim i As IntegerCombol.Addltem 张三Combol.Addltem 李四”Combol.Addltem“王五”a(1)=张三”:a(2)=李四”:a(3)=”王五”b(l)=”浙江林学院“:b(2)=浙江工业大学”:b(3)=浙江大学”c(l)=311
28、300:c(2)=310027:c(3)=310014d(l)=63742212:d(2)=88394277:d(3)=88326531End SubPrivate Sub Checkl_Click()If Checkl.Value=1 ThenLabell.FontBold=TrueElseLabell.FontBold=FalseEnd IfEnd SubPrivate Sub Check2_Click()If Checkl.Value=1 ThenLabell.Fontltalic=TrueElseLabell.Fontltalic=FalseEnd IfEnd SubPrivate S
29、ub Optionl_Click()Labell.FontName=宋体”End SubPrivate Sub Option2_Click()Labell.FontName=楷体_gb2312”End SubPrivate Sub Commandl_Click()Dim a As String,i As Integer,n As IntegerDim s As Integer,q As Integer,1 As Integern=Len(Text1.Text)For i=1 To na=Mid(Textl.Text,i,1)If a=a”And a=A And a=0 And a List3.
30、List(j)Thent=List3.List(i):List3.List(i)=List3.List(j):List3.List(j)=tEnd IfNext j t iEnd SubPrivate Sub Form_Load()Dim i As Integerf n As Integern=InputBox(n n)For i=1 To nListl.Additem Int(Rnd*90)+10List2.Additem Int(Rnd*90)+10Next iEnd Sub第 9 章对话框和菜单1.判断题:x J J(题中应把flag改为flags)J Jxx V V x xx V xx
31、2.选择题:DDCCD BDCCB3.填空题1.-2.一级子菜单3.将顶级菜单设置为不可见4.popupmenu pmenu,0,x,y5.showfont6.工程、部件7.click8.代码窗口,click9.下拉式,弹出式打开/另存为、颜色,字体、打印、帮助4.程序阅读题打开对话框2、*.txt3、allfiles*.exe*.txt*.doc4、Checking,No File Selectd,重 试(R),取消5.程序填空题“c:winnt”commondialog 1 .showopen,loadpicture(commondialogl.filename)6.程序设计题1、界面如图
32、:代码如下:Private Sub Command 1 _Click()CommonDialog 1 .FileName=CommonDialog 1 .Flags=4096CommonDialogl.Action=1If CommonDialogl.FileName=,n ThenMsgBox No File Selectd,5+vbExclaElseList 1.Additem CommonDialog 1 .FileNameEnd IfEnd Sub2、代码如下:Private Sub a_Click()CommonDialog 1 .ShowColorText 1.ForeColor=
33、CommonDialog 1 .ColorEnd SubPrivate Sub b_Click()CommonDialogl.Flags=1CommonDialog 1 .ShowFontText 1.FontSize=CommonDialogl.FontSizeEnd SubPrivate Sub c_Click()CommonDialogl.Flags=1CommonDialogl.ShowFontTextl.FontName=CommonDialog 1 .FontNameEnd SubPrivate Sub d_Click()EndEnd Sub第 10章图形操作和图形控件一、判断题X
34、 V X X X V X V X X二、选择题BAADACAA三、填空题(1)pic 1 .Circle(picl.ScaleLeft+picl.ScaleWidth/2,picl.ScaleTop+picl.ScaleHeight/2),700(2)在图片框picturel中,以(800,1000)为圆心画一个半径为500的圆(3)Forml.Scale(-200,250)-(300,-100)(4)Autosize stretch四、程序阅读题1、窗体宽和高的1/4为起点,画一个边长为1000的正方形,并画出其正向对角线2、在图片框中交替画出实心和空心的矩形,空心矩形是黄色边框。实心矩形是
35、黄色边框,黑色填充。五、程序填空题1、(1)Screen.Width(2)Screen.HeightX(4)Y2、(5)Picture 1 .ForeColor=CommonDialog 1 .Color(6)Picture!.CurrentX=X(7)Picture 1 .CurrentY=Y(8)Picture 1.Line-(x,y)六、程序设计题(1)Private Sub Form_Click()Const Pi=3.1415926Dim i As SingleForm 1.Scale(0,1)-(2*Pi,-1)Form 1.Line(0,0)-(2*Pi,0)Form 1.Lin
36、e(0,-1)-(0,1)For i=0 To 2*Pi Step 0.0001Forml.PSet(i,Sin(i)Next iEnd Sub(2)Private Sub Form_Click()Dim r As Single,x As Single,y As Singlex=Form 1 .Scale Width/2y=Forml.ScaleHeight/2r=xIf r y Then r=yForml.Circle(Forml.ScaleLeft+Form 1 .ScaleWidth/2,Forml.ScaleTop+Forml.ScaleHeight/2),r,vbBlue,-2*3.
37、14,-3.14End SubPrivate Sub Form_Load()Forml.DrawWidth=2 2 磅,题目不对,起码为1磅以上的整数Forml.FillStyle=0,接上,堤的刻度在宽度中一般不用Form 1 .FillColor=vbRedEnd Sub(3)Private Sub Picture l_MouseUp(Button As Integer,Shift As Integer,X As Single,Y As Single)Picture 1.FillStyle=0Picture 1.FillColor=vbGreenPicture 1.Line(XI,yl)-
38、(X,Y),vbRed,BEnd Sub第 11章文件一、判断题1-5:X J J J X 6-10:V V X X X二、选择题1-5:ABBAD 6-10:DCAAD三、程序阅读题、-4、结果:print:ABC1234567,abcdefgn write:AVBVC“1234567,abcdefg”2、结果:y=5a=5y=15a=10y=30a=153、窗体匕36 25 16 9 4 1文件中:1 4 9 16 25 36结果:5101520253035404550556065707580859095100m=1050四、程序填空题1、patterndir 1 .path=drive
39、1 .drivefile 1 .path+“+file 1 .filenamefile 1 .path+file 1 .filename2、open c:stu.txt“for input as#1not eof(l)input#1 ,stuNum,stuname,stuMark3 open e:a33dataout.txt“for output as#1tl mod t2=0if t2int(sqr(tl)print coun五、程序设计题1、Private Sub Form_click()Dim i As Integer,a(1 to 10)As Integer,j As Integer,
40、t As Integeri=0Open“c:a.txt”For Input As#1 打开有文件Do while not e o f(1)读取文件中的数据放到数组中i=i+1Input#1,a(i)loopClose#1Open c:b.txt”For Input As#2Do while not eof(2)i=i+1Input#2,a(i)loopClose#2For i=1 To 9,排序For j=i+1 To 10If a(i)a(j)Thent=a(i):a(i)=a(j):a(j)=tEnd IfNext jNext iOpen c:c.txt For Output As#3 把
41、排序好的数组放到新建的文件中For i=1 To 10Write#3,a(i)Next iClose#3End Sub2、Private Sub Form_dick()Dim num(l To 20)As String,定义相应的数组Dim chengjil(l To 20)As IntegerDim chengji2(l To 20)As IntegerDim sum(l To 20)As IntegerDim i As Integer,j As Integeri=0Open k:a.txt”For Input As#1 打开并读文件数据放到相应的数组中Do While Not EOF(l)
42、i=i+1Input#1,num(i),chengjil(i),chengji2(i)sum(i)=chengjil(i)+chengji2(i)LoopClose#1For i=1 To 19 冒泡法排列数组Forj=1 To 20-iIf sum(j)sum(j+1)Thent=sum(j)sum(j)=sum(j+1)sum(j+1)=tt=num(j)num(j)=num(j+1)num(j+1)=tt=chengjil(j)chengjil(j)=chengji l(j+1)chengjil(j+1)=tt=chengji2(j)chengji2(j)=chengji2(j+1)ch
43、engji2(j 4-1)=tEnd IfNext jNext iOpen k:b.txt For Output As#2,把排序好的数据放到(写到)新建的文件中For i=1 To 20Write#2,num(i),chengjil(i),chengji2(i),sum(i)Next iClose#2End Sub3、Private Sub Command 1 _Click()Open c:student.dat For Append As#1If Option 1.Value=True ThenWrite#1,Text 1.Text,Text2.Text,Text3.Text,Text4.Text,Text5.TextElseWrite#1,Text 1.Text,Text2.Text,女”,Text3.Text,Text4.Text,Text5.TextEnd IfText 1.Text=,HText2.Text=nText3.Text=Text4.Text=,H,Text5.Text=uClose#1End SubPrivate Sub Command2_Click()EndEnd Sub
限制150内