admin 管理员组文章数量: 887021
2023年12月22日发(作者:adguard)
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$<"输入字符串">
For I = 1 To Int
Mid = Mid
Mid = 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
1 / 27
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
Else
MsgBox
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 2 / 27 6、求s=a+aa+aaa+...aaaaa〔n个a〕,其中a和n的值随机产生,a的范围是[1,9]的整数,n的范围是[如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 n = Int 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<> 3 / 27 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 4 / 27 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 = 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 = s2 = s Mod 10 s3 = s 100 If s1 ^ 3 + s2 ^ 3 + s3 ^ 3 = s Then a = s + a 5 / 27 End If Next i Print a End Sub 12、随机产生一个三位正整数,然后逆序输出,产生的数与逆序数同时显示. 例如,产生246,输出是642. Dim a%, m%, b%, c%, d% Private Sub Form_Click<> a = Int Print a d = * 100 b = - 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 b = Val c = Val If a + b > c And a + c > b And b + c > a Then MsgBox <"能构成三角形"> Else MsgBox <"不能构成三角形"> End If End Sub 14、已知数组a<>,编程删除a中第5个元素. 6 / 27 数组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> For i = 0 To n Print a; Next i Print For i = 5 To n a = a Next i n = n - 1 ReDim Preserve a For i = 0 To n Print a; Next i Print End Sub 15、随机生成一个整型的二维数组,范围在[10,20]之间,以上三角形式输出该数组.Dim a%<4, 4> Private Sub Command1_Click<> For i = 0 To 4 For j = i To 4 Tab Next j Next i 7 / 27 〔下三角、全部元 End Sub Private Sub Form_Load<> For i = 0 To 4 For j = 0 To 4 a = Int 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 = Int Print Tab 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]内的整数,求它的最大值与所对8 / 27 Private Sub Form_Click<> Dim a%<3, 3> Min = 80 For i = 0 To 3 For j = 0 To 3 a = Int Print Tab If a <= Min Then Min = a: 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=inputbox<"a"> Next i For i =0 to 4 b=len Next i Max=1 For i =0 to 4 If max then max=b:imax=i Next i 9 / 27 Print "max=";a 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 = Int Print Tab 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 To UBound a = InputBox<"请输入字符串"> a; Spc<1>; Next i End Sub Private Sub Command2_Click<> For i = 1 To n For j = 1 To n - i 10 / 27 If a Next j Next i For i = 1 To n a; 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 = Int Next i amin = a<1> amax = a<1> avera = a<1> For i = 2 To 10 If a > amax Then amax = a If a < amin Then amin = a avera = avera + a Next i For i = 1 To 10 Print a; Next i Print 11 / 27 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<> Dim a%<3, 3>, b%<3, 3>, c%<3, 3> For i = 0 To 3 For j = 0 To 3 Tab Next j Next i End Sub Private Sub Command2_Click<> For i = 0 To 3 For j = 0 To 3 b; ""; Next j Next i End Sub Private Sub Command3_Click<> For i = 0 To 3 For j = 0 To 3 Tab Next j 12 / 27 Next i End Sub Private Sub Form_Load<> 23、随机产生15个小写字母,放在字符数组中.提示:c=chr Private Sub Command1_Click<> Dim c$<14>, i% For i = 0 To 14 c = Chr Next i For i = 0 To 14 c; 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 = Int Next i maxa = a<1> For i = 2 To 10 If a > a<1> Then maxa = a: m = i End If Next i For i = 1 To 10 Print a; 13 / 27 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 = Int a; Next j Next i For i = 0 To 3 For j = 0 To 3 b = Int b; Next j Next i For i = 0 To 3 For j = 0 To 3 c = a + b Next j Next i End Sub 26、利用随机函数生成一个4×4的矩阵<即二维矩阵>,范围是[20,50]内的整数,求它的两条对角线上Private Sub Form_Click<> 14 / 27 Dim a%<3, 3> For i = 0 To 3 For j = 0 To 3 a = Int Print Tab 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 = Int Print Tab If a <= Min Then Min = a: b = i: c = j End If Next j Next i Print Print "最小值为"; Min Print "其下标为"; "<"; b, c; ">" End Sub 28、随机生成一个整型的二维数组,范围在[10,20]之间,以上三角形式输出该数组.〔下三角、全部元15 / 27 Dim a%<4, 4> Private Sub Command1_Click<> For i = 0 To 4 For j = i To 4 Tab Next j Next i End Sub Private Sub Form_Load<> For i = 0 To 4 For j = 0 To 4 a = Int 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> For i = 0 To n Print a; Next i Print min = a<0>: imin = 0 For i = 0 To n 16 / 27 If a < min Then min = a: imin = i Next i Print "min="; "imin="; imin For i = imin + 1 To n a = a Next i ReDim Preserve a For i = 0 To n - 1 If a > max Then max = a: imax = i Next i Print "max="; "imax="; imax ReDim Preserve a s = 0 For i = 0 To n - 2 s = s + a Print a; Next i Print Print "avr="; s / End Sub 30、编程输出n行杨辉三角图形,n由键盘输入. Private Sub Command1_Click<> n = ReDim a<1 To n, 1 To n> For i = 1 To n For j = 1 To n a = 1 If i = j Then a = 1 Next j 17 / 27 Next i For i = 2 To n For j = 2 To n a = a + a Next j Next i For i = 1 To n For j = 1 To i a; " "; Next j Next i End Sub 31、声明一个整型的二维数组a<1 to 4,1 to 4>,用随机函数产生各元素,范围介于[1,20]之间, 编程将第1行和第3行对应元素交换. Dim a%<1 To 4, 1 To 4>, t% Private Sub Command1_Click<> For i = 1 To 4 For j = 1 To 4 Tab Next j Next i End Sub Private Sub Command2_Click<> For i = 1 To 4 For j = 1 To 4 t = a<1, j>: a<1, j> = a<3, j>: a<3, j> = t 18 / 27 Tab Next j Next i End Sub Private Sub Form_Load<> For i = 1 To 4 For j = 1 To 4 a = Int Next j Next i End Sub 32、随机产生15个小写字母,放在字符数组中.提示:c=chr Private Sub Command1_Click<> Dim c$<14>, i% For i = 0 To 14 c = Chr Next i For i = 0 To 14 c; Next i End Sub 33、编写一个过程计算并输出区间[100,1000]内所有素数的和<要求判断素数使用过程〔函数或子过提示:只能被1和自身整除的自然数称为素数.> Function sushu% Dim m%, Tag As Boolean, i% m = Val Tag = True 19 / 27 For i = 2 To m - 1 If Next i If Tag Then sushu = x End Function Private Sub Command1_Click<> Dim i%, k% For i = 100 To 1000 If sushu Then m i Next i Sum = 0 For k = 0 To unt - 1 Sum = Sum + Val< Next k Label1 = "[100,1000]内所有素数和为:" & Sum End Sub 34、随机产生10个1~100的正整数放入数组,显示产生的数,求最大值、最小值、平均值. Private Sub Form_Click<> Dim a<1 To 10> As Integer, i%, maxa%, mina%, suma% For i = 1 To 10 a = Int Next i mina = a<1> maxa = a<1> suma = a<1> For i = 2 To 10 If a > maxa Then maxa = a If a < mina Then mina = a suma = suma + a 20 / 27 Next i For i = 1 To 10 Print a; Next i Print Print "max="; maxa, "min="; mina; "aver="; suma / 10 End Sub 35、已知一维数组a<>中的元素已排序,分别为{12,15,21,25,27,35,36,39,48,52}, 编程将30插入数组a,并使a依然有序. Private Sub Command1_Click<> Dim a<>, i%, k%, x%, n% a = Array<12, 15, 21, 25, 27, 35, 36, 39, 48, 52> x = Val For k = 0 To n If x < a Next k ReDim Preserve a For i = n To k Step -1 a = a Next i a For i = 0 To n + 1 Print a; Next i End Sub 37、声明一个整型的二维数组a<1 to 4,1 to 4>,用随机函数产生各元素,范围介于[1,20]之间, 编程将第2行和第4行对应元素交换. Private Sub Form_Click<> 21 / 27 Dim a<0 To 3, 0 To 3>, i%, j%, t For i = 0 To 3 For j = 0 To 3 a = Int Print Tab Next j Next i Print Print For j = 0 To 3 t = a<1, j>: a<1, j> = a<3, j>: a<3, j> = t Next j For i = 0 To 3 For j = 0 To 3 Print Tab Next j Next i End Sub 38、随机产生10个[30,100]内的整数,求最小值与所对应的下标. Private Sub Form_Click<> Dim a<1 To 10> As Integer, i%, mina%, m% For i = 1 To 10 a = Int Next i mina = a<1> For i = 2 To 10 If a < a<1> Then mina = a: m = i ElseIf a = a<1> Then 22 / 27 mina = a: m = i End If Next i For i = 1 To 10 Print a; Next i Print "mina=" & mina; "下标为" & m End Sub 39、编一个判断宿舍的通用过程〔函数或子过程〕.调用该过程, 计算并输出区间〔1000,,1100〕内所有素数的和. Function ss Dim i%, tag As Boolean tag = True For i = 3 To n / 2 If Next i If tag Then ss = n End Function Private Sub Picture1_Click<> Dim i%, avera avera = 0 For i = 1000 To 1100 If ss <> 0 Then Print ss avera = avera + ss Next i avera End Sub 40、编一子过程ProcMin,求一维数组a中的最小值,子过程的形参自己确定. 23 / 27 Private Sub Form_Click<> Dim a<1 To 10>, amin, i% For i = 1 To 10 a = Int Print a; Next i Call ProcMin, amin> Print Print "amin ="; amin End Sub Sub ProcMin, min> Dim i% min = b For i = LBound + 1 To UBound If b < min Then min = b Next i End Sub 41、编一个冒泡排序法子过程,对已知的若干整数按递减次序排列.提示:子过程的形式为sub sort1提示:子过程的形式为sub sort1>. Private Sub Command1_Click<> Dim b%<10>, i% Print "排序前" For i = 0 To 9 b = Int Print b; Next i Print Print "排序后" Call sort> 24 / 27 End Sub Sub sort> Dim i%, m%, n%, min%, j% For i = 0 To n - 1 For j = 0 To n - 1 - i If a Next j Next i Print a; Next i End Sub 42、编一个选择排序法子过程,对已知的若干整数按递增次序排列.提示:子过程的形式为sub sort1Private Sub Command1_Click<> Dim b%<10>, i% Print "排序前" For i = 1 To 10 b = Int Print b; Next i Print Print "排序后" Call sort> End Sub Sub sort> Dim i%, m%, n%, min%, t% 25 / 27 For i = 0 To n - 1 min = i For j = i + 1 To n If a Next j m = a: a = a Next i For i = LBound To UBound Print a; Next i End Sub 43、编一个冒泡排序法子过程,对已知的若干整数按递增次序排列.提示:子过程的形式为sub sort1Private Sub Command1_Click<> Dim b%<10>, i% Print "排序前" For i = 0 To 9 b = Int Print b; Next i Print Print "排序后" Call sort> End Sub Sub sort> Dim i%, m%, n%, min%, j% For i = 0 To n - 1 For j = 0 To n - 1 - i If a 26 / 27 Next j Next i For i = LBound + 1 To UBound Print a; Next i End Sub 27 / 27 10
版权声明:本文标题:VB编程程序代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1703219846h442787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论