vb编程代码-.pdf
《vb编程代码-.pdf》由会员分享,可在线阅读,更多相关《vb编程代码-.pdf(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、编写程序,随机生成100个两位整数,并统计出其中小于等于 40、大于40小于等于 70及大于 70的数据个数。代码Private Sub Command1_Click()Dim x As Integer,i As Integer Dim cnt1 As Integer,cnt2 As Integer,cnt3 As Integer cnt1=0:cnt2=0:cnt3=0 Randomize For i=1 To 100 x=Int(Rnd*90+10)If x=40 Then cnt1=cnt1+1 ElseIf x 70 Then cnt2=cnt2+1 Else cnt3=cnt3+1
2、End If Next i Print cnt1,cnt2,cnt3 End Sub 编写程序,单击窗体时在窗体上输出如下所示的图形。代码Private Sub Form_Click()Dim i As Integer,begin As Integer begin=5 For i=1 To 5 Print Tab(begin);For j=1 To 2*i-1 Print*;Next j Print begin=begin-1 Next i begin=2 For i=4 To 1 Step-1 Print Tab(begin);or j=1 To 2*i-1 Print*;Next j Pr
3、int begin=begin+1 Next i End Sub 计算二的n 次幂代码Private Sub Command1_Click()Dim n As Integer,mi As Double Dim i As Integer n=Int(Val(InputBox()mi=1 For i=1 To n mi=mi*2 Next i Print 2 的;n;次幂为:;mi End Sub 编写程序,计算并输出下面级数前n 项(n=80)的部分和。1-1/2+1/3-1/4+1/5+1/n代码Inputbox 中输入 2,出现上述结果Private Sub Command1_Click()
4、Dim n As Integer,i As Integer Dim sum As Double n=Val(InputBox()sum=0 For i=1 To n If i Mod 2 0 Then sum=sum+(1/i)Else sum=sum-(1/i)End If Next i Print sum End Sub 有 100 匹马,驮100 担货:大马驮3 担,中马驮 2 担,两匹小马驮1 担,问有大、中小马各多少?代码显示结果如上所示Private Sub Form_Click()Dim x As Integer,y As Integer,z As Integer Print 大
5、马,中马,小马 For x=0 To 33 For y=0 To 50 z=100-x-y If(x*3+y*2+z*0.5)=100 Then Print x,y,z End If Next y Next x 编写程序,要求从键盘输入0-6 中的任一个,可对应得到星期日、星期一-星期六代码Inputbox 中输入 2 结果如上Dim n As Integer n=Int(Val(InputBox()Select Case n Case 0 Print n;:星期日 Case 1 Print n;:星期一 Case 2 Print n;:星期二 Case 3 Print n;:星期三 Case
6、 4 Print n;:星期四 Case 5 Print n;:星期五 Case 6 Print n;:星期六 Case Else Print n;:错误 End Select End Sub 编写程序,输出下列三角形:(要求用循环做,非循环不给分。)代码运行结果如上图所示Private Sub Command1_Click()Dim i As Integer,j As Integer,begin As Integer begin=7 For i=1 To 4 Print Tab(begin);For j=1 To 2*i-1 Print 1;注意:双引号中 1 后面必须有一个空格Next j
7、 Print begin=begin-2 Next i End Sub 求公式:1/1!+1/2!+1/3!+1/n!直到最后一项小于10(-6)时 e 的值。(注:写在Command1 的 Click 事件中。)代码运行结果如上图所示Private Sub Command1_Click()Dim sum As Double,n As Long,nj As Double Dim i As Long sum=0 n=1 Do nj=1 For i=1 To n nj=nj*i Next i sum=sum+1/nj n=n+1 Loop While(1/nj)=0.000001 Print su
8、m End Sub 编写程序,输出下列三角形:总行数通过键盘输入。代码Inputbox 输入 5 运行结果如上图所示Option Explicit Private Sub Command1_Click()Dim i As Integer,j As Integer,n As Integer,begin As Integer n=Int(Val(InputBox()begin=1 For i=1 To n Print Tab(begin);For j=1 To 9-2*i Print 1;Next j Print begin=begin+2 Next i End Sub 输入一串数字,求其中偶数位
9、数字之和。例如输入 1357986,则求 3+7+8 代码Inputbox 输入 5 运行结果如上图所示Private Sub Command1_Click()Dim str As String,s As String*1,i As Long,sum As Long str=InputBox()Print str sum=0 For i=2 To Len(str)Step 2 s=Mid(str,i,1)sum=sum+Val(s)Print s Next i 输入一串数字,求其中奇数数字构成的数与偶数数字构成的数之差。例如输入 1654238,求 1536428。代码Inputbox 输入
10、5 运行结果如上图所示Private Sub Command1_Click()Dim str As String,s1 As String,s2 As String,i As Long str=InputBox()Print str s1=s2=For i=1 To Len(str)If i Mod 2=1 Then s1=s1&Mid(str,i,1)Else s2=s2&Mid(str,i,1)End If Next i Print s1;-;s2;=;Val(s1)-Val(s2)End Sub 编写程序,单击窗体时在窗体上输出如下所示的图形。代码Private Sub Command1
11、_Click()Dim begin As Integer,n As Integer,i As Long begin=9 For i=1 To 5 Print Tab(begin);For n=1 To 2*i-1 Print*;Next n Print begin=begin-2 Next i begin=3 For i=1 To 4 Print Tab(begin);For n=1 To 9-2*i Print*;Next n Print begin=begin+2 Next i End Sub 有一分数序列:1,1/2,2/3,3/5,5/8,8/13 求出这个数列的前20 项之和。代码O
12、ption Explicit Private Sub Command1_Click()Dim sum As Double Dim a1 As Long,a2 As Long,a3 As Long Dim b1 As Long,b2 As Long,b3 As Long Dim i As Long a1=1:a2=1 b1=1:b2=2 sum=a1/b1+a2/b2 For i=3 To 20 a3=a1+a2 b3=b1+b2 sum=sum+a3/b3 a1=a2 a2=a3 b1=b2 b2=b3 Next i Print sum End Sub 编写程序,计算下面级数前n 项中(n=5
13、0)奇数项的和,并输出该级数最后两项之差的绝对1*2*3+2*3*4+3*4*5+n*(n+1)*(n+2)+代码Option Explicit Private Sub Command1_Click()Dim sum As Long,dif As Long,n As Long sum=0 For n=1 To 49 Step 2 sum=sum+n*(n+1)*(n+2)Next n dif=(49-52)*50*51 Print sum Print Abs(dif)End Sub 编写程序,计算并输出下面级数前n 项(n=30)的部分和。1/(1*2)+1/(2*3)+1/(3*4)+1/(
14、n*(n+1)+代码Option Explicit Private Sub Command1_Click()Dim sum As Double,n As Long sum=0 For n=1 To 30 sum=sum+1/(n*(n+1)Next n Print sum End Sub 编写程序,输出下列三角形:代码编程打印如下图形,总行数通过键盘输入。(要求用循环做,非循环不给分。按原图形直接打印者无效。)代码Inputbox 输入 5 运行结果如上图所示Option Explicit Private Sub Command1_Click()Dim f1 As Long,f2 As Lon
15、g,f3 As Long Dim n As Long,begin As Long Dim i As Long,j As Long n=Int(Val(InputBox()begin=4*n-3 f1=1 f2=1 For i=1 To n f3=f1+f2 Print Tab(begin);For j=1 To 2*i-1 Print Trim(f3);Spc(4-Len(Trim(f3);相邻两数的位置差4 Next j Print begin=begin-4 f1=f2 f2=f3 Next i End Sub Option Explicit Private Sub Command1_Cl
16、ick()Dim code1 As Integer,code2 As Integer For code1=Asc(G)To Asc(A)Step-1 For code2=code1 To Asc(G)Print Chr(code2);Next code2 Print Next code1 End Sub 勾股定理中三个数的关系是:a2+b2=c2 编写程序,输出100 以内满足上述关系的整数组合,例如3,4,5 就是一个整数组合。代码Press enter star this 还有两列结果未显示Option Explicit Private Sub Command1_Click()Dim a
17、As Integer,b As Integer,c As Double Dim cnt cnt=0 For a=1 To 99 For b=1 To 99 c=Sqr(a*a+b*b)If c 100 And Int(c)=c Then Print a;b;c;,cnt=cnt+1 If cnt Mod 4=0 Then Print End If Next b Next a End Sub 通过键盘输入任意长度的字符串,要求将字符 顺 序 倒 置,例 如,将 输 入 的HEJISUANJISHIYANSHI变换成IHSNAYIHSIJNAUSIJEH。代码先后输入5 和 a 后显示结果如上Op
18、tion Explicit Private Sub Command1_Click()Dim s1 As String,s2 As String Dim i As Long s1=InputBox(请输入原始字符串)s2=For i=Len(s1)To 1 Step-1 s2=s2&Mid(s1,i,1)Next i Print s1 Print s2 End Sub 求 1000 以内分别被3、5、7 除,余数都为1的正整数的个数及其和代码Option Explicit Private Sub Command1_Click()Dim n As Integer,sum As Double Dim
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vb 编程 代码
限制150内