vb编程代码演示教学.doc
《vb编程代码演示教学.doc》由会员分享,可在线阅读,更多相关《vb编程代码演示教学.doc(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流vb编程代码代码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; 次幂为:; miEnd Sub编写程序,随机生成100个两位整数,并统计出其中小于等于40、大于40小于等于70及大于70的数据个数。代码Private Sub Command1_Click() Dim x As Integer, i
2、 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 End If Next i Print cnt1, cnt2, cnt3End Sub编写程序,单击窗体时在窗体上输出如下所示的图形。代码P
3、rivate 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 Print begin = begin + 1 Next iEnd Sub编写
4、程序,计算并输出下面级数前n项(n=80)的部分和。 1-1/2+1/3-1/4+1/5+1/n代码Inputbox中输入2,出现上述结果Private Sub Command1_Click() 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 sumEnd Sub有100匹马,驮100担货:大马驮3
5、担,中马驮2担,两匹小马驮1担,问有大、中小马各多少?代码显示结果如上所示Private Sub Form_Click() Dim x As Integer, y As Integer, z As Integer Print 大马, 中马, 小马 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 结果
6、如上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 4 Print n; :星期四 Case 5 Print n; :星期五 Case 6 Print n; :星期六 Case Else Print n; :错误 End SelectEnd Sub编写程序,输出下列三角形: (要求用循环做,非循环不给分。)代码运行结果如上图所示Private Sub Comm
7、and1_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 Print begin = begin - 2 Next iEnd Sub求公式:1/1!+1/2!+1/3!+1/n! 直到最后一项小于10(-6)时e的值。(注:写在Command1的Click事件中。)代码运行结果如上图所示Private Sub Command1_C
8、lick() 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 sumEnd Sub编写程序,输出下列三角形: 总行数通过键盘输入。代码Inputbox输入5 运行结果如上图所示Option ExplicitPrivate Sub Command1_Click() Dim i As In
9、teger, 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 iEnd Sub输入一串数字,求其中偶数位数字之和。例如输入1357986,则求3+7+8代码Inputbox输入5 运行结果如上图所示Private Sub Command1_Click() Dim str As Strin
10、g, 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输入5 运行结果如上图所示Private Sub Command1_Click() Dim str As String, s1 As String, s2 As
11、 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_Click() Dim begin As Integer, n As Integ
12、er, 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 iEnd Sub有一分数序列:1 , 1/2 , 2/3 , 3/5 , 5/8 , 8/13 求出这个数列的前20项之和。
13、代码Option ExplicitPrivate 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 =
14、 b3 Next i Print sumEnd Sub编写程序,计算下面级数前n项中(n=50)奇数项的和,并输出该级数最后两项之差的绝对1*2*3+2*3*4+3*4*5+n*(n+1)*(n+2)+代码Option ExplicitPrivate 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
15、 Abs(dif)End Sub编程打印如下图形,总行数通过键盘输入。(要求用循环做,非循环不给分。按原图形直接打印者无效。)代码Inputbox输入5 运行结果如上图所示Option ExplicitPrivate Sub Command1_Click() Dim f1 As Long, f2 As Long, 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
16、 + 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编写程序,计算并输出下面级数前n项(n=30)的部分和。 1/(1*2)+1/(2*3)+1/(3*4)+1/(n*(n+1)+代码Option ExplicitPrivate Sub Command1_Click() Dim sum As Double, n As Long sum =
17、 0 For n = 1 To 30 sum = sum + 1 / (n * (n + 1) Next n Print sumEnd Sub编写程序,输出下列三角形: 代码Option ExplicitPrivate Sub Command1_Click() 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 code1End Sub勾股定理中三个数的关系是:
18、a2 + b2 =c2 编写程序,输出100以内满足上述关系的整数组合,例如3,4,5就是一个整数组合。代码Press enter star this 还有两列结果未显示Option ExplicitPrivate Sub Command1_Click() Dim a 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 =
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vb 编程 代码 演示 教学
限制150内