2022年vb编程代码.pdf
《2022年vb编程代码.pdf》由会员分享,可在线阅读,更多相关《2022年vb编程代码.pdf(15页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、vb 编程代码编写程序 ,随机生成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 7
2、0 Then cnt2 = cnt2 + 1 Else cnt3 = cnt3 + 1 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 F
3、or i = 4 To 1 Step -1 Print Tab(begin); or j = 1 To 2 * i - 1 Print *; Next j Print 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
4、 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 1 页,共 15 页 - - - - - - - - - - vb 编程代码编写程序 ,计算并输出下面级数前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
5、 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 大马 , 中马 , 小马 For x = 0 To 33 For y = 0 To 50 z = 100 - x - y I
6、f (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 4 Print n; : 星期四
7、Case 5 Print n; : 星期五 Case 6 Print n; : 星期六 Case Else Print n; : 错误 End Select End Sub 编写程序 ,输出下列三角形: (要求用循环做,非循环不给分。) 代码精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 15 页 - - - - - - - - - - vb 编程代码运行结果如上图所示Private Sub Command1_Click() Dim i As Integer, j As Integer, be
8、gin 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 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
9、 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 sum End Sub 编写程序,输出下列三角形: 总行数通过键盘输入。代码Inputbox 输入 5 运行结果如上图所示Option Explicit Private Sub Command1_Click() Dim i As Integer, j As Integer, n
10、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 输入一串数字 ,求其中偶数位数字之与。例如输入 1357986,则求 3+7+8 代码精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 3 页,共 15 页 - -
11、 - - - - - - - - vb 编程代码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,求 153
12、6428。代码Inputbox 输入 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(
13、s2) End Sub 编写程序 ,单击窗体时在窗体上输出如下所示的图形。代码Private Sub Command1_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 *
14、; Next n Print begin = begin + 2 Next i End Sub 有一分数序列 :1 , 1/2 , 2/3 , 3/5 , 5/8 , 8/13 求出这个数列的前20 项之与。代码精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 4 页,共 15 页 - - - - - - - - - - vb 编程代码Option Explicit Private Sub Command1_Click() Dim sum As Double Dim a1 As Long, a2 As Lo
15、ng, 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=50) 奇数项的与,并输出该级数最后两项之差的绝对1*2*3+2*3*4+3
16、*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 编程打印如下图形,总行数通过键盘输入。(要求用循环做 ,非循环不给分。按原图形直接打印者无效。 ) 代码Inputbox 输入
17、 5 运行结果如上图所示Option Explicit Private 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 + f2 Print Tab(begin); For j = 1 To 2 * i - 1 Print Trim(f3); Spc(4 - Le
18、n(Trim(f3); 相邻两数的位置差4 Next j Print begin = begin - 4 f1 = f2 f2 = f3 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 5 页,共 15 页 - - - - - - - - - - vb 编程代码编写程序 ,计算并输出下面级数前n 项(n=30)的部分与。1/(1*2)+1/(2*3)+1/(3*4)+1/(n*(n+1)+代码Option Explicit Private Sub Command1_Click() Dim sum As D
19、ouble, n As Long sum = 0 For n = 1 To 30 sum = sum + 1 / (n * (n + 1) Next n Print sum End Sub 编写程序 ,输出下列三角形: 代码Option Explicit Private 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
20、 code1 End Sub 勾股定理中三个数的关系就是:a2 + b2 =c2 编写程序 ,输出 100 以内满足上述关系的整数组合 ,例如 3,4,5 就就是一个整数组合。代码Press enter star this 还有两列结果未显示Option Explicit Private 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)
21、 = 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变换成代码Next i End Sub 精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 6 页,共 15 页 - - - - - - - - - - vb 编程代码IHSNAYIHSIJNAUSIJEH。
22、先后输入 5 与 a 后显示结果如上Option 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_Cl
23、ick() Dim n As Integer, sum As Double Dim cnt As Integer sum = 0 cnt = 0 For n = 1 To 1000 If (n Mod 3 = 1) And (n Mod 5 = 1) And (n Mod 7 = 1) Then Print n cnt = cnt + 1 sum = sum + n End If Next n Print Print 共; cnt; 个 Print 与为 ; sum End Sub 代码图形 1_Click() 图形 4_Click() Private Sub 图形 1_Click() Cls
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022 vb 编程 代码
限制150内