vb编程代码演示教学.doc
如有侵权,请联系网站删除,仅供学习与交流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 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编写程序,单击窗体时在窗体上输出如下所示的图形。代码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 Print begin = begin + 1 Next iEnd Sub编写程序,计算并输出下面级数前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担,中马驮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 结果如上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 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 Print begin = begin - 2 Next iEnd 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 sumEnd Sub编写程序,输出下列三角形: 总行数通过键盘输入。代码Inputbox输入5 运行结果如上图所示Option ExplicitPrivate 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 iEnd Sub输入一串数字,求其中偶数位数字之和。例如输入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输入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_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 iEnd Sub有一分数序列:1 , 1/2 , 2/3 , 3/5 , 5/8 , 8/13 求出这个数列的前20项之和。代码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 = 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 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 + 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 = 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勾股定理中三个数的关系是: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 = cnt + 1 If cnt Mod 4 = 0 Then Print End If Next b Next aEnd Sub通过键盘输入任意长度的字符串,要求将字符顺序倒置,例如,将输入的"HEJISUANJISHIYANSHI"变换成"IHSNAYIHSIJNAUSIJEH"。 代码先后输入5和a后显示结果如上Option ExplicitPrivate 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 s2End Sub求1000以内分别被3、5、7除,余数都为1的正整数的个数及其和代码Option ExplicitPrivate Sub Command1_Click() 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 "和为" sumEnd Sub代码图形1_Click()图形4_Click()图形6_Click()水仙花数_Click()图形2_Click()图形3_Click()图形5_Click()乘积表_Click()数列_Click()阶梯数_Click()图形7_Click()Private Sub 图形1_Click()ClsPrint For n = 1 To 8 Print Tab(2 * n + 2); For m = 1 To 8 Print Spc(1); "*" Next Print NextEnd SubPrivate Sub 图形4_Click()ClsFor i = 1 To 5 Print Tab(20 - 3 * i); For j = 1 To 2 * i - 1 Print i; Next j PrintNext iEnd SubPrivate Sub 图形6_Click()Cls k = 1For i = 1 To 6 For j = 1 To i Print Tab(5 * j); k; k = k + 1 Next j PrintNext iEnd SubPrivate Sub 水仙花数_Click()'一个三位数,如果它等于各位数的立方和,'如153=13+53+33,称为水仙花数。求所有的水仙花数。ClsFor i = 1 To 9 For j = 0 To 9 For k = 0 To 9 a = 100 * i + 10 * j + k b = i 3 + j 3 + k 3 If a = b Then Print Tab(4); a Next k Next jNext iEnd SubPrivate Sub 图形2_Click()Cls Print For n = 1 To 5 Print Tab(n + 2); For m = n To 10 - n Print "*" Next Print Spc(4); For m = 1 To 2 * n - 1 Print "*" Next Print NextEnd SubPrivate Sub 图形3_Click()Cls Print For n = 1 To 6 Print Tab(8 - n); For m = 1 To 2 * n - 1 Print "*" Next m Next n For n = 1 To 5 Print Tab(n + 2); For m = n To 10 - n Print "*" Next m Next nEnd SubPrivate Sub 矩阵_Click()Cls Dim a(10, 10) As IntegerDim m As Integer, n As Integer For n = 1 To 10 For m = 1 To 10 If n = m Or n = 11 - m Then a(n, m) = 1 Else a(n, m) = 0 End If Next Next Print For n = 1 To 10 For m = 1 To 10 Print Tab(m * 3); a(n, m); Next Print NextEnd SubPrivate Sub 图形5_Click()Cls' 编写程序,输出下列三角形: (要求用循环做,非循环不给分)' G ' FG ' EFG ' DEFG ' CDEFG 'BCDEFG 'ABCDEFGDim a As String, i As Integer, k As Integera = "ABCDEFG"k = Len(a)For i = 1 To k Print Tab(3); Right(a, i)Next iEnd SubPrivate Sub 乘积表_Click()' 编写程序,打印如下的乘积表:'* 3 6 9 12'15'16'17'18Dim i As Integer, j As IntegerClsPrint " * 3 6 9 12"For i = 15 To 18 Print i; " " For j = 3 To 12 Step 3 Print i * j; " " Next j PrintNext iEnd SubPrivate Sub 数列_Click()'有一数列:1、1、2、3、5、8、13、21,求出这个数列的前20项之和ClsDim f1 As Single, f2 As Single, f3 As SingleDim i As Integer, sum As Singlef1 = 1f2 = 1Print Tab(3); f1Print Tab(3); f2sum = 2Do While i <= 20 - 3 f3 = f1 + f2 f1 = f2 f2 = f3 sum = sum + f3 i = i + 1 Print Tab(3); f3Loop Print Tab(3); "其和是:" sumEnd SubPrivate Sub 阶梯数_Click()'有一个长阶梯,如果每步跨2阶最后剩1阶,'如果每步跨3阶最后剩2阶,如果每步跨4阶最后剩3阶,'如果每步跨5阶最后剩4阶,如果每步跨6阶最后剩5阶,'只有当如果每步跨7阶时恰好走完,问这个阶梯至少有多少阶?Dim a As Integer, i As IntegerClsDo While True i = i + 1 If i Mod 2 = 1 And i Mod 3 = 2 And i Mod 4 = 3 And i Mod 5 = 4 And i Mod 6 = 5 And i Mod 7 = 0 Then Print Tab(3); "该阶梯至少有" i; "阶" Exit Do End IfLoopEnd SubPrivate Sub 图形7_Click()' 编写程序,输出下列三角形: (要求用循环做,非循环不给分)' 河 ' 河南 ' 河南农 ' 河南农业 ' 河南农业大 '河南农业大学 ClsDim a As String, i As Integer, k As Integera = "河南农业大学"k = Len(a)For i = 1 To k Print Tab(3); Left(a, i)Next iEnd Sub阶乘累加和代码Private Sub Form_Click() Dim sum As Long, jc As Long Dim i As Integer, n As Integer sum = 0 For n = 1 To 10 jc = 1 For i = 1 To n jc = jc * i Next i sum = sum + jc Next n Print sumEnd Sub求高次方根代码Option ExplicitPrivate Sub Form_Click() '说明:此题题目中要求的区间【1,2】有误,忽略之 Dim x As Double, fx As Double, fpx As Double, d As Double x = 1.5 Do d = (3 * x * x * x - 4 * x * x - 5 * x + 13) / (9 * x * x - 8 * x - 5) x = x - d Loop Until Abs(d) <= 0.0001 Print "方程的根是 " xEnd Sub数字三角形代码输入5后运行结果如图Private Sub Form_Click() Dim i As Long, j As Long Dim n As Long, begin As Long n = Val(InputBox("请输入总行数") begin = 2 * n - 1 For i = 1 To n Print Tab(begin); For j = 1 To (2 * i - 1) Print Trim(2 * i - 1); " " Next j Print begin = begin - 2 Next iEnd Sub求阶梯数目;每步跨2阶,剩余1;跨3剩2;4剩3;5剩4;6剩5,7不剩代码运行结果如图Private Sub Command1_Click()Dim n%n = 7Do While True If n Mod 2 = 1 And n Mod 3 = 2 And n Mod 4 = 3 And n Mod 5 = 4 And n Mod 6 = 5 And n Mod 7 = 0 Then Exit Do End If n = n + 1 Loop Print nEnd Sub代码Private Sub Form_Click() Dim i As Long, n As Long Dim cnt As Long, sum As Long Dim flag As Boolean cnt = 0 sum = 0 For n = 100 To 1000 flag = True For i = 2 To n - 1 If n Mod i = 0 Then flag = False Exit For End If Next i If flag = True Then cnt = cnt + 1 sum = sum + n Print n, If cnt Mod 5 = 0 Then Print End If Next n Print Print Print "找到" cnt; "个质数," "和为" sumEnd Sub球从100处自由落,每次反弹回原高度一半,再落,第十次落地后经历?m,最后一次反弹高度代码Private Sub Command1_Click()Dim s!, d!, i!s = 0d = 100For i = 1 To 10s = s + d + d / 2d = d / 2Next iPrint "总共距离" s; "最后一次的高度" dEnd Sub321米钢料,a种17米,b种27米,求ab各多少段,余料最少为多少代码Private Sub Command1_Click()Dim i%, j%, min%, m%, n%min = 321For i = 1 To 321 / 17For j = 1 To 321 / 27If 321 - i * 17 - j * 27 < min And 321 - i * 17 - j * 27 >= 0 Thenmin = 321 - i * 17 - j * 27m = in = jEnd IfNext jNext iPrint "17米" m; "段", "27米" n; "段", "最小残料" minEnd Sub猴子每天吃掉一半多一个,到第七天只剩一个,求桃子总数代码Private Sub Command1_Click()Dim x1%, x2%, i%x1 = 1For i = 6 To 1 Step -1x2 = (x1 + 1) * 2Print "第" i; "天桃子个数为:" x2x1 = x2Next iEnd Sub评委打分,去掉最高、最低,输出平均分代码Private Sub Command1_Click()Dim s%, a%, min%, max%, i%min = 100max = 0s = 0For i = 1 To 7a = InputBox("分数:")Print a;s = s + aIf a > max Then max = aIf a < min Then min = aNext iPrintPrint max, minPrint "平均分" (s - max - min) / 5End Sub输入任意字符串,然后倒置显示代码Private Sub Command1_Click()Dim a As StringDim i%a = Text1.TextPrint aFor i = 1 To Len(a)Print Mid(a, Len(a) - i + 1, 1)Next