VB编程程序代码.docx
- 文档编号:24413692
- 上传时间:2023-05-27
- 格式:DOCX
- 页数:28
- 大小:24.79KB
VB编程程序代码.docx
《VB编程程序代码.docx》由会员分享,可在线阅读,更多相关《VB编程程序代码.docx(28页珍藏版)》请在冰豆网上搜索。
VB编程程序代码
VB编程程序代码
1、求100以内的素
Private Sub Form_Click()
Dim i%, j%
For i = 2 To 100
For j = 2 To i - 1
If i Mod j = 0 Then Exit For
Next j
If j = i Then Print i
Next i
End Sub
2、从键盘输入任意长度的字符串,要求将字符顺序倒置
例如,将输入的“ABCDEFG”变换成“GFEDCBA”。
Private Sub Command1_Click()
Dim a$, I%, c$, d$, n%
a = InputBox$("输入字符串")
n = Len(a)
For I = 1 To Int(n \ 2)
c = Mid(a, I, 1)
Mid(a, I, 1) = Mid(a, n - I + 1, 1)
Mid(a, n - I + 1, 1) = c
Next I
Print a
End Sub
3、计算0~200之间所有能被11或5整除的数之和
Private Sub Form_Click()
Dim n%, i%
n = 0
For i = 1 To 200
If i Mod 11 = 0 Or i Mod 5 = 0 Then
n = n + i
End If
Next i
Print n
End Sub
4、输入一年份,判断它是否为闰年,并显示有关信息。
(判断闰年的条件是:
年份能被4整除但不能被100整除,或者能被400整除)
Private Sub Command1_Click()
Dim y%
y = InputBox("请输入年数")
If y Mod 4 = 0 And y Mod 100 <> 0 Or y Mod 400 = 0 Then
MsgBox (y & "年是闰年")
Else
MsgBox (y & "年是平年")
End If
End Sub
5、已知x,y,z 3个变量中存放了3个不同的数,比较它们的大小并进行调整,使得x Private Sub Command1_Click() Dim x! y! z! x = InputBox("inputx") y = InputBox("inputy") z = InputBox("inputz") Print " x y z" If x > y Then t = x: x = y: y = t If x > z Then t = x: x = z: z = t If y > z Then t = y: y = z: z = t Print "排序后"; x; " "; y; " "; z End Sub 6、求s=a+aa+aaa+...aaaaa(n个a),其中a和n的值随机产生,a的范围是[1,9]的整数,n的范围是[5,10]。 如a=3,n=6,则s=3+33+333+3333+33333+333333。 编程确定n和a的值,并计算s。 Private Sub Form_Click() Dim s! t! i! a%, n% a = Int(Rnd * 9 + 1) n = Int(Rnd * 6 + 5) t = 0: s = 0 Print "a="; a, "n="; n For i = 1 To n t = t * 10 + a s = s + t Print t; Next i Print Print "s="; s End Sub 7、计算100~300之间所有能被3和7整除的数之和。 Private Sub Form_Click() For i = 100 To 300 If i Mod 21 = 0 Then s = s + i Print i; End If Next i Print Print "s="; s End Sub 8、编程求200--400范围内5的倍数或7的倍数之和。 (一个数如果同时是7和5的倍数,则只能加一次。 ) Private Sub Command1_Click() Dim s1%,s2%,s3%,i% S1=0 S2=0 For i=200 to 400 If i mod 5 =0 or i mod 7=0 then s1=s1+i End if Next i For i=200 to 400 If i mod 5 =0 or i mod 7=0 then s2=s2+i End if Next i S3=s1-s2 Print s3 End sub 9、找出被3、5、7除,余数为1的最小的5个正整数。 Private Sub Command1_Click() Dim CountN%, n% CountN = 0 n = 1 Do n = n + 1 If n Mod 3 = 1 And n Mod 5 = 1 And n Mod 7 = 1 Then Print n CountN = CountN + 1 End If Loop Until CountN = 5 End Sub 10、某次歌曲大奖赛,有7个评委。 如果分别输入7个评委对某个参赛者的打分数, 按照去掉一个最高分和一个最低分的计算办法,求出该参赛者的平均得分。 Private Sub Command1_Click() Dim mark! max! min! aver! i% aver = 0 For i = 1 To 7 mark = InputBox("输入第" & i & "位评委的打分") If i = 1 Then max = mark: min = mark Else If mark < min Then min = mark If mark > max Then max = mark End If aver = aver + mark Next i aver = (aver - min - max) / 5 Print aver End Sub 11、编程显示100~500之间所有的水仙花数之和。 (水仙花数是3位数,其各位数之和等于该数本身) Private Sub Form_Click() Dim i%, s%, s1%, s2%, s3%, a! a = 0 For i = 0 To 400 s = 99 + i s1 = (s Mod 100) \ 10 s2 = s Mod 10 s3 = s \ 100 If s1 ^ 3 + s2 ^ 3 + s3 ^ 3 = s Then a = s + a End If Next i Print a End Sub 12、随机产生一个三位正整数,然后逆序输出,产生的数与逆序数同时显示。 例如,产生246,输出是642。 Dim a%, m%, b%, c%, d% Private Sub Form_Click() a = Int(Rnd * 900 + 100) Print a d = (a Mod 10) * 100 b = (a Mod 100) - (a Mod 10) c = a \ 100 m = c + b + d Print m End Sub 13、从键盘输入三角形的三条边a,b,c的值,根据其数值,判断能否构成三角形。 Private Sub Form_Click() Dim a%, b%, c% a = Val(InputBox("input a")) b = Val(InputBox("input b")) c = Val(InputBox("input c")) If a + b > c And a + c > b And b + c > a Then MsgBox ("能构成三角形") Else MsgBox ("不能构成三角形") End If End Sub 14、已知数组a(),编程删除a中第5个元素。 数组a中的元素分别为{12,6,4,89,75,63,100,20,31}。 Private Sub Form_Click() Dim a(), i%, n% a = Array(12, 6, 4, 89, 75, 63, 100, 20, 31) n = UBound(a) For i = 0 To n Print a(i); Next i Print For i = 5 To n a(i - 1) = a(i) Next i n = n - 1 ReDim Preserve a(n) For i = 0 To n Print a(i); Next i Print End Sub 15、随机生成一个整型的二维数组,范围在[10,20]之间,以上三角形式输出该数组。 (下三角、全部元素) Dim a%(4, 4) Private Sub Command1_Click() Picture1.Cls For i = 0 To 4 For j = i To 4 Picture1.Print Tab(j * 6); a(i, j); Next j Picture1.Print Next i End Sub Private Sub Form_Load() For i = 0 To 4 For j = 0 To 4 a(i, j) = Int(Rnd * 11 + 10) Next j Next i End Sub 16、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,输出每行中的最大值和下标。 Private Sub Form_Click() Dim a%(3, 3), s0%, s1%, s2%, s3%, b0%, b1%, b2%, b3% Max = 40 For i = 0 To 3 For j = 0 To 3 a(i, j) = Int(Rnd * 31 + 20) Print Tab(j * 5); a(i, j); If a(0, j) >= Max Then s0 = a(0, j): b0 = j If a(1, j) >= Max Then s1 = a(1, j): b1 = j If a(2, j) >= Max Then s2 = a(2, j): b2 = j If a(3, j) >= Max Then s3 = a(3, j): b3 = j Next j Next i Print Print " 第一行"; s0; "("; 0; b0; ")" Print " 第二行"; s1; "("; 1; b1; ")" Print " 第三行"; s2; "("; 2; b2; ")" Print " 第四行"; s3; "("; 3; b3; ")" End Sub 17、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[40,80]内的整数,求它的最大值及所对应的下标。 Private Sub Form_Click() Dim a%(3, 3) Min = 80 For i = 0 To 3 For j = 0 To 3 a(i, j) = Int(Rnd * 41 + 40) Print Tab(j * 5); a(i, j); If a(i, j) <= Min Then Min = a(i, j): b = i: c = j End If Next j Next i Print Print "最小值为"; Min Print "其下标为"; "("; b, c; ")" End Sub 18、输入一系列字符串,编程求出长度最大的字符串并输出其所对应的次序。 Private Sub Form_Click() Dim a(5) as string,b(4) as integer,t%,max%,imax% For i =1 to 5 a(i)=inputbox("a") Next i For i =0 to 4 b(i)=len(trim(a(i))) Next i Max=1 For i =0 to 4 If max imax=i Next i Print "max=";a(i) Print "cixu=",imax+1 End sub 19、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,输出该矩阵所有数据之和。 Private Sub Form_Click() Dim a%(3, 3) For i = 0 To 3 For j = 0 To 3 a(i, j) = Int(Rnd * 31 + 20) Print Tab(j * 5); a(i, j); Next j Next i s = a(0, 0) + a(1, 1) + a(2, 2) + a(3, 3) + a(3, 0) + a(2, 1) + a(1, 2) + a(0, 3) Print s; End Sub 20、输入一系列字符串,将字符串按递减次序排列。 请编程实现。 Dim a() As String, n%, i%, j% Private Sub Command1_Click() n = Text1 ReDim a(1 To n) As String For i = LBound(a) To UBound(a) a(i) = InputBox("请输入字符串") Picture1.Print a(i); Spc (1); Next i End Sub Private Sub Command2_Click() For i = 1 To n For j = 1 To n - i If a(j) < a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = t Next j Next i For i = 1 To n Picture2.Print a(i); Spc (1); Next i End Sub Private Sub Form_Load() Text1 = "" End Sub 21、随机产生10个[30,100]内的整数,求最大值、最小值和平均值。 Dim a(1 To 10) As Integer, amin, amax, i%, avera! Private Sub Form_Click() For i = 1 To 10 a(i) = Int(Rnd * 71 + 30) Next i amin = a (1) amax = a (1) avera = a (1) For i = 2 To 10 If a(i) > amax Then amax = a(i) If a(i) < amin Then amin = a(i) avera = avera + a(i) Next i For i = 1 To 10 Print a(i); Next i Print Print "max="; amax, "min="; amin, "avera="; avera / 10 End Sub 22、定义三个4×4的二维数组A,B,C,A和B中的元素均随机生成,数组A的范围是1~20, 数组B的范围是100~200,数组C是A和B相乘得到的。 请编程生成并输出A,B,C。 (相加) Private Sub Command1_Click() Picture1.Cls Dim a%(3, 3), b%(3, 3), c%(3, 3) For i = 0 To 3 For j = 0 To 3 Picture1.Print Tab(j * 6); a(i, j); Next j Picture1.Print Next i End Sub Private Sub Command2_Click() Picture2.Cls For i = 0 To 3 For j = 0 To 3 Picture2.Print b(i, j); ""; Next j Picture2.Print Next i End Sub Private Sub Command3_Click() Picture3.Cls For i = 0 To 3 For j = 0 To 3 Picture3.Print Tab(j * 7); c(i, j); Next j Picture3.Print Next i End Sub Private Sub Form_Load() 23、随机产生15个小写字母,放在字符数组中。 提示: c=chr(int(rnd*26+97)) Private Sub Command1_Click() Dim c$(14), i% Picture1.Cls For i = 0 To 14 c(i) = Chr(Int(Rnd * 26 + 97)) Next i For i = 0 To 14 Picture1.Print c(i); Next i End Sub 24、随机产生10个[30,100]内的整数,求最大值及所对应的下标 Private Sub Form_Click() Dim a(1 To 10) As Integer, i%, maxa%, m% For i = 1 To 10 a(i) = Int(Rnd * 70) + 30 Next i maxa = a (1) For i = 2 To 10 If a(i) > a (1) Then maxa = a(i): m = i End If Next i For i = 1 To 10 Print a(i); Next i Print "maxa=" & maxa; "下标为" & m End Sub 25、定义三个4×4的二维数组A,B,C,A和B中的元素均随机生成,数组A范围是1~20, 数组B的范围是100~200,数组C是A和B相加得到的。 请编程生成并输出A,B,C。 Private Sub Command1_Click() Dim a(3, 3), b(3, 3), c(3, 3), t%, m% For i = 0 To 3 For j = 0 To 3 a(i, j) = Int(Rnd * 20 + 1) Picture1.Print a(i, j); Next j Next i Picture1.Print For i = 0 To 3 For j = 0 To 3 b(i, j) = Int(Rnd * 101 + 100) Picture1.Print b(i, j); Next j Next i Picture1.Print For i = 0 To 3 For j = 0 To 3 c(i, j) = a(i, j) + b(i, j) Next j Next i End Sub 26、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[20,50]内的整数,求它的两条对角线上元素之和。 Private Sub Form_Click() Dim a%(3, 3) For i = 0 To 3 For j = 0 To 3 a(i, j) = Int(Rnd * 31 + 20) Print Tab(j * 5); a(i, j); Next j Next i s = a(1, 1) + a(2, 2) + a(3, 3) + a(0, 0) + a(0, 3) + a(1, 2) + a(2, 1) + a(3, 0) Print Print "两对角线上元素之和为: "; s End Sub 27、利用随机函数生成一个4×4的矩阵(即二维矩阵),范围是[40,80]内的整数,求它的最小值及所对应的下标。 Private Sub Form_Click() Dim a%(3, 3) Min = 80 For i = 0 To 3 For j = 0 To 3 a(i, j) = Int(Rnd * 41 + 40) Print Tab(j * 5); a(i, j); If a(i, j) <= Min Then Min = a(i, j): b = i: c = j End If Next j Next i Print Print "最小值为"; Min Print "其下标为"; "("; b, c; ")" End Sub 28、随机生成一个整型的二维数组,范围在[10,20]之间,以上三角形式输出该数组。 (下三角、全部元素) Dim a%(4, 4) Private Sub Command1_Click() Picture1.Cls For i = 0 To 4 For j = i To 4 Picture1.Print Tab(j * 6); a(i, j); Next j Picture1.Print Next i End Sub Private Sub Form_Load() For i = 0 To 4 For j = 0 To 4 a(i, j) = Int(Rnd * 11 + 10) Next j Next i End Sub 29、有10个评委对歌手进行打分(分数存在A数组中)要求编程计算出歌手平均得分 (按去掉一个最高分和一个最低分的计算方法计算。 )a = Array(98, 97, 95, 91, 90, 99, 93, 94, 93, 96) Private Sub Form_Click() Dim a(), max%, min%, imax%, imin%, s! a = Array(98, 97, 95, 91, 90, 99, 93, 94, 93, 96) n = UBound(a) For i = 0 To
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB 编程 程序代码
![提示](https://static.bdocx.com/images/bang_tan.gif)