vb第四章选择结构和循环结构编程题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第四章编程题1.查询分数题PrivateSubForm_Load()x=Val(InputBox(请输入分数,输入,0))SelectCasexCaseIs0Label1.Caption=输入有误CaseIs100Label1.Caption=输入有误CaseIs=90Label1.Caption=优秀CaseIs=80Label1.Caption=良好CaseIs=70Label1.Caption=一般CaseIs=60Label1.Caption=及格CaseIs60Label1.Caption=不及格EndSelectEndSub2.求最大公约数题PrivateSubCommand1_Click()DimmAsInteger,nAsInteger,pAsIntegerm=Val(Text1.Text)n=Val(Text2.Text)Ifm0Orn0ThenMsgBox数据错误!EndEndIfp=mModnDoWhilepom=nn=pp=mModnLoopText3.Text=nEndSub2.1最小公倍数PrivateSubCommand1_Click()DimmAsInteger,nAsInteger,pAsInteger,tAsIntegerm=Val(Text1.Text)n=Val(Text2.Text)t=m*nIfm0Orn0ThenMsgBox数据错误!EndEndIfIfm=0Orn=0ThenText3.Text=0ElseDop=mModnIfp=0ThenExitDoEndIfm=nn=pLoopUntilFalseText3.Text=t/nEndIfEndSub3.九九乘法表PrivateSubCommand1_Click()DimIAsInteger,JAsInteger,MAsInteger,sAsStringForI=1To9s=ForJ=1ToIM=I*Js=s&J&*&I&=&M&NextJList1.AddItemsNextIEndSub4.求ABCD—CDC=ABC题PrivateSubCommand1_Click()Fora=1To9:Forb=0To9Forc=1To9:Ford=0To9x=a*1000+b*100+10*c+dy=c*100+d*10+cz=a*100+b*10+cIfx-y=zThenPrintx;y;zEndIfNextNextNextNextEndSub5.求前100项和,平均数题PrivateSubForm_click()n=1:s=0DoWhilen=100s=s+nn=n+1LoopPrints,n-1,s/(n-1),s*(n-1)EndSub6.输入10个数找出正数,负数,零的个数PrivateSubCommand1_Click()Dimi,t,x1,x2,x3AsIntegerFori=1To10t=Val(InputBox(请输入第&i&个数:,显示,0))Ift0Thenx1=x1+1ElseIft0Thenx2=x2+1Elsex3=x3+1EndIfNextPrint正数的个数:&x1Print负数的个数:&x2Print零的个数:&x3EndSub7.Fibonacci数列(用数组做)PrivateSubForm_Load()Fori=1To30List1.AddItemFib(i)NextEndSubPrivateFunctionFib(n)Ifn3ThenFib=1ElseFib=Fib(n-1)+Fib(n-2)EndIfEndFunctionPrivateSubList1_Click()List1.AddItem=FibEndSub8.输入十个数打印并找出最大数,最小数PrivateSubCommand1_Click()Dima(9),i,max,minAsIntegerFori=0To9a(i)=Val(InputBox(请输入第&i+1&个数:))Printa(i);Nextmax=a(0)Fori=0To9Ifa(i)maxThenmax=a(i)Ifa(i)minThenmin=a(i)NextPrintPrintPrintPrint最大数为:&max;最小数为:&minEndSub9.100元换成40张等值的10,5,2,1的钱的题PrivateSubCommand1_Click()Forx=1To10Fory=1To20Forz=1To50w=40-x-y-zIf10*x+5*y+2*z+w=100Andx*y*z*w0Thenh=Format(x,@@@@)&Format(y,@@@@@)_&Format(z,@@@@@)&Format(w,@@@@@)List1.AddItemhEndIfNextzNextyNextxEndSub10.找出三位数的回文数并打印,并求其个数PrivateSubCommand1_Click()DimnAsIntegerForn=101To999a=Int(n/100)b=Int((n-a*100)/10)c=n-a*100-b*10Ifa=cThenList1.AddItemnNextPrintList1.ListCountEndSub10-2回文字判断1)按定义判断先求出字符串的反序字符串,然后和原字符串比较,如果相等则是回文,否则不是回文。DimstrAsString,iAsInteger'以下求Text1中文本的反序串strFori=1ToLen(Text1)str=Mid(Text1,i,1)&strNextiIfstr=Text1ThenMsgBox是回文ElseMsgBox不是回文(2)首尾字符的成对比较将字符串折半比较,若每对字符都相等则是回文,否则只要有一对字符不等就不是回文。DimiAsIntegerFori=1ToLen(Text1)\2IfMid(Text1,i,1)Mid(Text1,Len(Text1)-i+1,1)ThenExitForNextiIfiLen(Text1)\2ThenMsgBox是回文ElseMsgBox不是回文11.求1-10之间阶乘题PrivateSubCommand1_Click()DimiAsInteger,sAsDoublei=1DoWhilei=10s=1Forj=1Tois=s*jNextjPrinti;!=;si=i+1LoopEndSub12.水仙花题PrivateSubCommand1_Click()DimiAsInteger,mAsIntegerFori=100To999m=(iMod10)^3+_(i\10Mod10)^3+(i\100)^3Ifm=iThenPrintiNextiEndSubPrivateSubCommand2_Click()Fora=1To9:Forb=0To9:Forc=0To9x=a*100+b*10+cIfx=a^3+b^3+c^3ThenPrintxEndIfNextNextNextEndSub13.打印菱形PrivateSubCommand1_Click()Fori=1To5PrintSpc(6-i);Forj=1ToiPrintSpc(1);*;NextjPrintNextiFori=5To1Step-1PrintSpc(6-i);Forj=1ToiPrintSpc(1);*;NextjPrintNextiEndSub14.用Do循环设计例4.11中的s=1×2×3×…×n的值,其中n由输入对话框获得。PrivateSubCommand1_Click()n=InputBox(请输入n的值:,输入)s=1i=1DoIfiVal(n)ThenExitDos=s*ii=i+1LoopMsgBoxs=+Str(s),0,计算结果EndSub15.求由数字0、1、2、3、4组成的所有无重复数字的3位正整数。PrivateSubCommand1_Click()DimaAsInteger,bAsInteger,cAsInteger,nAsIntegerFora=1To4Forb=0To4Forc=0To4IfabAndbcAndacThenPrinta*100+b*10+c;或为List1.AddItema*100+b*10+cn=n+1IfnMod5=0ThenPrintEndIfNextcNextbNextaEndSub课后题1.PrivateSubCommand1_Click()x=Val(InputBox(请输入一个数,显示,0))Ifx0Thenx=MsgBox(是正数,48,显示)Ifx=0Thenx=MsgBox(是零,48,显示)Ifx0Thenx=MsgBox(是负数,64,显示)EndSub2.PrivateSubCommand1_Click()DimaAsSingleDimbAsSingleDimcAsSingleDimtAsSinglea=Val(InputBox(请输入第一个数,从大到小排序,0))b=Val(InputBox(请输入第二个数,从大到小排序,0))c=Val(InputBox(请输入第三个数,从大到小排序,0))IfabThent=a:a=b:b=tIfacThent=a:a=c:c=tIfbcThent=b:b=c:c=tPrint从大到小的顺序为:;a;b;cEndSub3.PrivateSubCommand1_Click()StaticnAsIntegerx=Text1.TextIfx=123ThenLabel1.Caption=欢迎进入本系统ElseLabel1.Caption=输入错误n=n+1Ifn=3ThenEndEndIfEndSubPrivateSubCommand2_Click()Text1.Text=EndSub4.PrivateSubCommand1_Click()IfLeft(Text1.Text,2)=1ThenText2.Text=一年级ElseIfLeft(Text1.Text,2)=2ThenText2.Text=二年级ElseIfLeft(Text1.Text,2)=3ThenText2.Text=三年级ElseIfLeft(Text1.Text,2)=4ThenText2.Text=四年级ElseText2.Text=输入错误EndIfEndIfEndIfEndIfIfMid(Text1.Text,5,1)=2ThenText3.Text=博士生ElseIfMid(Text1.Text,5,1)=3ThenText3.Text=硕士生ElseIfMid(Text1.Text,5,1)=4ThenText3.Text=本科生ElseIfMid(Text1.Text,5,1)=5ThenText3.Text=专科生ElseText3.Text=输入错误EndIfEndIfEndIfEndIfEndSub5.PrivateSubCommand1_Click()DimxAsIntegerForx=100To200IfxMod3=0OrxMod5=0ThenList1.AddItemxEndIfNextPrintList1.ListCountEndSub6.PrivateSubCommand1_Click()a=1:b=1List1.AddItemaList1.AddItembForn=1To28c=a+ba=bb=cList1.AddItemcNextEndSub7.完备数PrivateSubCommand1_Click()Dimm,i,j,sj=结果:Form=2To1000s=0Fori=1Tom-1IfmModi=0Thens=s+iNextIfs=mThenj=j&m&,NextPrintjEndSubPrivateSubCommand2_Click()DimmAsInteger,sAsIntegerForm=2To10

1 / 11
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功