您好,欢迎访问三七文档
编写程序,随机生成100个两位整数,并统计出其中小于等于40、大于40小于等于70及大于70的数据个数。代码PrivateSubCommand1_Click()DimxAsInteger,iAsIntegerDimcnt1AsInteger,cnt2AsInteger,cnt3AsIntegercnt1=0:cnt2=0:cnt3=0RandomizeFori=1To100x=Int(Rnd*90+10)Ifx=40Thencnt1=cnt1+1ElseIfx70Thencnt2=cnt2+1Elsecnt3=cnt3+1EndIfNextiPrintcnt1,cnt2,cnt3EndSub编写程序,单击窗体时在窗体上输出如下所示的图形。代码PrivateSubForm_Click()DimiAsInteger,beginAsIntegerbegin=5Fori=1To5PrintTab(begin);Forj=1To2*i-1Print*;NextjPrintbegin=begin-1Nextibegin=2Fori=4To1Step-1PrintTab(begin);orj=1To2*i-1Print*;NextjPrintbegin=begin+1NextiEndSub计算二的n次幂代码PrivateSubCommand1_Click()DimnAsInteger,miAsDoubleDimiAsIntegern=Int(Val(InputBox()))mi=1Fori=1Tonmi=mi*2NextiPrint2的;n;次幂为:;miEndSub编写程序,计算并输出下面级数前n项(n=80)的部分和。1-1/2+1/3-1/4+1/5+……+1/n代码Inputbox中输入2,出现上述结果PrivateSubCommand1_Click()DimnAsInteger,iAsIntegerDimsumAsDoublen=Val(InputBox())sum=0Fori=1TonIfiMod20Thensum=sum+(1/i)Elsesum=sum-(1/i)EndIfNextiPrintsumEndSub有100匹马,驮100担货:大马驮3担,中马驮2担,两匹小马驮1担,问有大、中小马各多少?代码显示结果如上所示PrivateSubForm_Click()DimxAsInteger,yAsInteger,zAsIntegerPrint大马,中马,小马Forx=0To33Fory=0To50z=100-x-yIf(x*3+y*2+z*0.5)=100ThenPrintx,y,zEndIfNextyNextx编写程序,要求从键盘输入0-6中的任一个,可对应得到星期日、星期一---星期六代码Inputbox中输入2结果如上DimnAsIntegern=Int(Val(InputBox()))SelectCasenCase0Printn;:星期日Case1Printn;:星期一Case2Printn;:星期二Case3Printn;:星期三Case4Printn;:星期四Case5Printn;:星期五Case6Printn;:星期六CaseElsePrintn;:错误EndSelectEndSub编写程序,输出下列三角形:(要求用循环做,非循环不给分。)代码运行结果如上图所示PrivateSubCommand1_Click()DimiAsInteger,jAsInteger,beginAsIntegerbegin=7Fori=1To4PrintTab(begin);Forj=1To2*i-1Print1;'注意:双引号中1后面必须有一个空格NextjPrintbegin=begin-2NextiEndSub求公式:1/1!+1/2!+1/3!+……+1/n!直到最后一项小于10^(-6)时e的值。(注:写在Command1的Click事件中。)代码运行结果如上图所示PrivateSubCommand1_Click()DimsumAsDouble,nAsLong,njAsDoubleDimiAsLongsum=0n=1Donj=1Fori=1Tonnj=nj*iNextisum=sum+1/njn=n+1LoopWhile(1/nj)=0.000001PrintsumEndSub编写程序,输出下列三角形:总行数通过键盘输入。代码Inputbox输入5运行结果如上图所示OptionExplicitPrivateSubCommand1_Click()DimiAsInteger,jAsInteger,nAsInteger,beginAsIntegern=Int(Val(InputBox()))begin=1Fori=1TonPrintTab(begin);Forj=1To9-2*iPrint1;NextjPrintbegin=begin+2NextiEndSub输入一串数字,求其中偶数位数字之和。例如输入1357986,则求3+7+8代码Inputbox输入5运行结果如上图所示PrivateSubCommand1_Click()DimstrAsString,sAsString*1,iAsLong,sumAsLongstr=InputBox()Printstrsum=0Fori=2ToLen(str)Step2s=Mid(str,i,1)sum=sum+Val(s)PrintsNexti输入一串数字,求其中奇数数字构成的数与偶数数字构成的数之差。例如输入1654238,求153-6428。代码Inputbox输入5运行结果如上图所示PrivateSubCommand1_Click()DimstrAsString,s1AsString,s2AsString,iAsLongstr=InputBox()Printstrs1=s2=Fori=1ToLen(str)IfiMod2=1Thens1=s1&Mid(str,i,1)Elses2=s2&Mid(str,i,1)EndIfNextiPrints1;-;s2;=;Val(s1)-Val(s2)EndSub编写程序,单击窗体时在窗体上输出如下所示的图形。代码PrivateSubCommand1_Click()DimbeginAsInteger,nAsInteger,iAsLongbegin=9Fori=1To5PrintTab(begin);Forn=1To2*i-1Print*;NextnPrintbegin=begin-2Nextibegin=3Fori=1To4PrintTab(begin);Forn=1To9-2*iPrint*;NextnPrintbegin=begin+2NextiEndSub有一分数序列:1,1/2,2/3,3/5,5/8,8/13……求出这个数列的前20项之和。代码OptionExplicitPrivateSubCommand1_Click()DimsumAsDoubleDima1AsLong,a2AsLong,a3AsLongDimb1AsLong,b2AsLong,b3AsLongDimiAsLonga1=1:a2=1b1=1:b2=2sum=a1/b1+a2/b2Fori=3To20a3=a1+a2b3=b1+b2sum=sum+a3/b3a1=a2a2=a3b1=b2b2=b3NextiPrintsumEndSub编写程序,计算下面级数前n项中(n=50)奇数项的和,并输出该级数最后两项之差的绝对1*2*3+2*3*4+3*4*5+…+n*(n+1)*(n+2)+…代码OptionExplicitPrivateSubCommand1_Click()DimsumAsLong,difAsLong,nAsLongsum=0Forn=1To49Step2sum=sum+n*(n+1)*(n+2)Nextndif=(49-52)*50*51PrintsumPrintAbs(dif)EndSub编写程序,计算并输出下面级数前n项(n=30)的部分和。1/(1*2)+1/(2*3)+1/(3*4)+…+1/(n*(n+1))+……代码OptionExplicitPrivateSubCommand1_Click()DimsumAsDouble,nAsLongsum=0Forn=1To30sum=sum+1/(n*(n+1))NextnPrintsumEndSub编写程序,输出下列三角形:代码编程打印如下图形,总行数通过键盘输入。(要求用循环做,非循环不给分。按原图形直接打印者无效。)代码Inputbox输入5运行结果如上图所示OptionExplicitPrivateSubCommand1_Click()Dimf1AsLong,f2AsLong,f3AsLongDimnAsLong,beginAsLongDimiAsLong,jAsLongn=Int(Val(InputBox()))begin=4*n-3f1=1f2=1Fori=1Tonf3=f1+f2PrintTab(begin);Forj=1To2*i-1PrintTrim(f3);Spc(4-Len(Trim(f3)));'相邻两数的位置差4NextjPrintbegin=begin-4f1=f2f2=f3NextiEndSubOptionExplicitPrivateSubCommand1_Click()Dimcode1AsInteger,code2AsIntegerForcode1=Asc(G)ToAsc(A)Step-1Forcode2=code1ToAsc(G)PrintChr(code2);Nextcode2PrintNextcode1EndSub勾股定理中三个数的关系是:a^2+b^2=c^2编写程序,输出100以内满足上述关系的整数组合,例如3,4,5就是一个整数组合。代码Pressenterstarthis还有两列结果未显示OptionExplicitPrivateSubCommand1_Click()DimaAsInteger,bAsInteger,cAsDoubleDimcntcnt=0Fora=1To99Forb=1To99c=Sqr(a*a+b*b)Ifc100AndInt(c)=cThenPrinta;;b;;c;,cnt=cnt+1IfcntMod4=0ThenPrintEndIfNextbNextaEndSub通过键盘输入任意长度的字符串,要求将字符顺序倒置,例如,将输入的HEJISUANJISHIYANSHI变换成IHSNAYIHSIJNAUSIJEH。代码先后输入5和a后显示结果如上OptionExplicitPrivateSubCommand1_Click()Dims1AsString,s2AsStringDimiAsLongs1=InputBox(请输入原始字符串)s2=Fori=Len(s1)To1Step-1s2=s2&Mid(s1,i,1)NextiPrints1Prints2EndSub求1000以内分别被3、5、7除,余数都为1的正整数的个数及其和代码OptionExplicitPrivateSubCommand1_Click()DimnAsInteger,sumAsDoubleDimcntAsIntegersum=0cnt=0Forn=1To1000If(nMod3=1)And(nMod5=1)And(nMod7=1)ThenPrintncnt=cnt+1sum=sum+nEndIfNextnPrintPrint共;cnt;个Print和为;sumEndSub代码图形1_Click()图形4_Click()PrivateSub图形1_Click()ClsPrintForn=1To8PrintTab(2*n+2);Form=1To8PrintSpc(1);*;NextPrintNextEndSubPrivateSub图形4_Click
本文标题:vb编程代码
链接地址:https://www.777doc.com/doc-1496811 .html