2022年VB编程程序代码.pdf
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 iNext iEnd Sub2、从键盘输入任意长度的字符串,要求将字符顺序倒置例如,将输入的 “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 Sub3、计算 0200之间所有能被 11或 5 整除的数之与Private Sub Form_Click()Dim n%, i%n = 0 For i = 1 To 200精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 1 页,共 27 页 - - - - - - - - - - VB编程程序代码 If i Mod 11 = 0 Or i Mod 5 = 0 Then n = n + i End If Next i Print nEnd Sub4、输入一年份 , 判断它就是否为闰年 , 并显示有关信息。(判断闰年的条件就是 : 年份能被 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 IfEnd Sub5、已知 x,y,z 3 个变量中存放了 3 个不同的数 , 比较它们的大小并进行调整, 使得 xy y Then t = x: x = y: y = tIf x z Then t = x: x = z: z = tIf y z Then t = y: y = z: z = tPrint 排序后 ; x; ; y; ; zEnd Sub精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 2 页,共 27 页 - - - - - - - - - - VB编程程序代码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(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 Sub7、计算 100300之间所有能被 3 与 7 整除的数之与。Private Sub Form_Click()For i = 100 To 300If i Mod 21 = 0 Thens = s + iPrint i;End IfNext iPrintPrint s=; sEnd Sub8、编程求 200-400范围内 5 的倍数或 7 的倍数之与。 (一个数如果同时就是7 与 5 的倍数 , 则只能加Private Sub Command1_Click()精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 3 页,共 27 页 - - - - - - - - - - VB编程程序代码 Dim s1%,s2%,s3%,i% S1=0S2=0For i=200 to 400If i mod 5 =0 or i mod 7=0 then s1=s1+iEnd ifNext i For i=200 to 400If i mod 5 =0 or i mod 7=0 then s2=s2+iEnd ifNext iS3=s1-s2Print s3End sub9、找出被 3、5、7 除, 余数为 1 的最小的 5 个正整数。Private Sub Command1_Click()Dim CountN%, n%CountN = 0n = 1Do 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 页 - - - - - - - - - - VB编程程序代码10、某次歌曲大奖赛 , 有 7 个评委。如果分别输入7 个评委对某个参赛者的打分数, 按照去掉一个最高分与一个最低分的计算办法, 求出该参赛者的平均得分。Private Sub Command1_Click()Dim mark!, max!, min!, aver!, i%aver = 0For i = 1 To 7 mark = InputBox(输入第 & i & 位评委的打分 )If i = 1 Then max = mark: min = markElse If mark max Then max = markEnd Ifaver = aver + markNext iaver = (aver - min - max) / 5Print averEnd Sub11、编程显示 100500之间所有的水仙花数之与。(水仙花数就是 3 位数, 其各位数之与等于该数本Private Sub Form_Click()Dim i%, s%, s1%, s2%, s3%, a!a = 0For i = 0 To 400s = 99 + is1 = (s Mod 100) 10s2 = s Mod 10s3 = s 100If s1 3 + s2 3 + s3 3 = s Thena = s + a精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 5 页,共 27 页 - - - - - - - - - - VB编程程序代码End IfNext iPrint aEnd Sub12、随机产生一个三位正整数, 然后逆序输出 , 产生的数与逆序数同时显示。例如, 产生 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 mEnd Sub13、从键盘输入三角形的三条边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 IfEnd Sub14、已知数组 a(), 编程删除 a中第 5 个元素。精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 6 页,共 27 页 - - - - - - - - - - VB编程程序代码数组 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 PrintFor i = 5 To n a(i - 1) = a(i)Next i n = n - 1ReDim Preserve a(n)For i = 0 To n Print a(i);Next iPrintEnd Sub15、随机生成一个整型的二维数组, 范围在 10,20之间, 以上三角形式输出该数组。(下三角、全部元Dim a%(4, 4)Private Sub Command1_Click()Picture1、ClsFor i = 0 To 4 For j = i To 4 Picture1 、Print Tab(j * 6); a(i, j); Next j Picture1 、PrintNext i精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 7 页,共 27 页 - - - - - - - - - - VB编程程序代码End SubPrivate Sub Form_Load()For i = 0 To 4 For j = 0 To 4 a(i, j) = Int(Rnd * 11 + 10) Next jNext iEnd Sub16、利用随机函数生成一个4 4 的矩阵 (即二维矩阵 ),范围就是 20,50内的整数 ,输出每行中的最大值Private Sub Form_Click()Dim a%(3, 3), s0%, s1%, s2%, s3%, b0%, b1%, b2%, b3%Max = 40For i = 0 To 3 For j = 0 To 3a(i, j) = Int(Rnd * 31 + 20)Print Tab(j * 5); a(i, j);If a(0, j) = Max Then s0 = a(0, j): b0 = jIf a(1, j) = Max Then s1 = a(1, j): b1 = jIf a(2, j) = Max Then s2 = a(2, j): b2 = jIf a(3, j) = Max Then s3 = a(3, j): b3 = jNext jNext iPrintPrint 第一行 ; s0; (; 0; b0; )Print 第二行 ; s1; (; 1; b1; )Print 第三行 ; s2; (; 2; b2; )Print 第四行 ; s3; (; 3; b3; )End Sub17、利用随机函数生成一个44 的矩阵 (即二维矩阵 ),范围就是 40,80内的整数 , 求它的最大值及所精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 8 页,共 27 页 - - - - - - - - - - VB编程程序代码Private Sub Form_Click()Dim a%(3, 3)Min = 80For i = 0 To 3 For j = 0 To 3a(i, j) = Int(Rnd * 41 + 40)Print Tab(j * 5); a(i, j);If a(i, j) = Min ThenMin = a(i, j): b = i: c = jEnd IfNext jNext iPrintPrint 最小值为 ; MinPrint 其下标为 ; (; b, c; )End Sub18、输入一系列字符串 ,编程求出长度最大的字符串并输出其所对应的次序。Private Sub Form_Click()Dim a(5) as string,b(4) as integer,t%,max%,imax%For i =1 to 5a(i)=inputbox(a)Next iFor i =0 to 4b(i)=len(trim(a(i)Next iMax=1For i =0 to 4If maxb(i) then max=b(i):imax=iNext i精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 9 页,共 27 页 - - - - - - - - - - VB编程程序代码Print max=;a(i)Print cixu=,imax+1End sub19、利用随机函数生成一个44 的矩阵 (即二维矩阵 ),范围就是 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 Sub20、输入一系列字符串 , 将字符串按递减次序排列。请编程实现。Dim a() As String, n%, i%, j%Private Sub Command1_Click()n = Text1ReDim a(1 To n) As StringFor i = LBound(a) To UBound(a)a(i) = InputBox(请输入字符串 )Picture1、Print a(i); Spc(1);Next iEnd SubPrivate Sub Command2_Click()For i = 1 To n For j = 1 To n - i精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 10 页,共 27 页 - - - - - - - - - - VB编程程序代码 If a(j) amax Then amax = a(i) If a(i) a(1) Then maxa = a(i): m = i End IfNext iFor i = 1 To 10 Print a(i);精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 13 页,共 27 页 - - - - - - - - - - VB编程程序代码Next i Print maxa= & maxa; 下标为 & mEnd Sub25、定义三个 44 的二维数组 A,B,C, A 与 B 中的元素均随机生成 , 数组 A 范围就是 120, 数组 B 的范围就是 100200, 数组 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 iEnd Sub26、利用随机函数生成一个4 4 的矩阵 (即二维矩阵 ),范围就是 20,50内的整数 , 求它的两条对角线上Private Sub Form_Click()精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 14 页,共 27 页 - - - - - - - - - - VB编程程序代码Dim a%(3, 3)For i = 0 To 3 For j = 0 To 3a(i, j) = Int(Rnd * 31 + 20)Print Tab(j * 5); a(i, j);Next jNext is = a(1, 1) + a(2, 2) + a(3, 3) + a(0, 0) + a(0, 3) + a(1, 2) + a(2, 1) + a(3, 0)PrintPrint 两对角线上元素之与为 :; sEnd Sub27、利用随机函数生成一个4 4 的矩阵 (即二维矩阵 ),范围就是 40,80内的整数 , 求它的最小值及所对Private Sub Form_Click()Dim a%(3, 3)Min = 80For i = 0 To 3 For j = 0 To 3a(i, j) = Int(Rnd * 41 + 40)Print Tab(j * 5); a(i, j);If a(i, j) = Min ThenMin = a(i, j): b = i: c = jEnd IfNext jNext iPrintPrint 最小值为 ; MinPrint 其下标为 ; (; b, c; )End Sub28、随机生成一个整型的二维数组,范围在 10,20之间, 以上三角形式输出该数组。(下三角、全部元精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 15 页,共 27 页 - - - - - - - - - - VB编程程序代码Dim a%(4, 4)Private Sub Command1_Click()Picture1、ClsFor i = 0 To 4 For j = i To 4 Picture1 、Print Tab(j * 6); a(i, j); Next j Picture1 、PrintNext iEnd SubPrivate Sub Form_Load()For i = 0 To 4 For j = 0 To 4 a(i, j) = Int(Rnd * 11 + 10) Next jNext iEnd Sub29、有 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 n Print a(i);Next iPrintmin = a(0): imin = 0For i = 0 To n精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 16 页,共 27 页 - - - - - - - - - - VB编程程序代码 If a(i) max Then max = a(i): imax = iNext iPrint max=; imax=; imaxReDim Preserve a(n - 2)s = 0For i = 0 To n - 2s = s + a(i)Print a(i);Next iPrintPrint avr=; s / (n - 1)End Sub30、编程输出 n 行杨辉三角图形 , n 由键盘输入。Private Sub Command1_Click()n = Text1 、TextReDim a(1 To n, 1 To n)For i = 1 To n For j = 1 To n a(i, 1) = 1 If i = j Then a(i, j) = 1 Next j精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 17 页,共 27 页 - - - - - - - - - - VB编程程序代码 Next i For i = 2 To n For j = 2 To n a(i, j) = a(i - 1, j - 1) + a(i - 1, j) Next j Next i For i = 1 To n For j = 1 To i Picture1 、Print a(i, j); ; Next j Picture1 、Print Next iEnd Sub31、声明一个整型的二维数组a(1 to 4,1 to 4), 用随机函数产生各元素, 范围介于 1,20之间, 编程将第 1 行与第 3行对应元素交换。Dim a%(1 To 4, 1 To 4), t%Private Sub Command1_Click()Picture1、ClsFor i = 1 To 4 For j = 1 To 4 Picture1 、Print Tab(j * 4); a(i, j); Next j Picture1 、PrintNext iEnd SubPrivate Sub Command2_Click()For i = 1 To 4For j = 1 To 4 t = a(1, j): a(1, j) = a(3, j): a(3, j) = t精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 18 页,共 27 页 - - - - - - - - - - VB编程程序代码 Picture2 、Print Tab(j * 4); a(i, j);Next j Picture2 、PrintNext iEnd SubPrivate Sub Form_Load()For i = 1 To 4 For j = 1 To 4 a(i, j) = Int(Rnd * 20 + 1) Next jNext iEnd Sub32、随机产生 15 个小写字母 , 放在字符数组中。提示 : c=chr(int(rnd*26+97)Private Sub Command1_Click()Dim c$(14), i%Picture1、ClsFor i = 0 To 14c(i) = Chr(Int(Rnd * 26 + 97)Next iFor i = 0 To 14Picture1、Print c(i);Next iEnd Sub33、编写一个过程计算并输出区间100, 1000内所有素数的与 (要求判断素数使用过程 ( 函数或子过示: 只能被 1 与自身整除的自然数称为素数。)Function sushu%(ByVal x%) Dim m%, Tag As Boolean, i% m = Val(x) Tag = True精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 19 页,共 27 页 - - - - - - - - - - VB编程程序代码 For i = 2 To m - 1 If (m Mod i) = 0 Then Tag = False Next i If Tag Then sushu = xEnd FunctionPrivate Sub Command1_Click()Dim i%, k%For i = 100 To 1000 If sushu(i) Then List1、AddItem iNext i Sum = 0 For k = 0 To List1、ListCount - 1 Sum = Sum + Val(List1、List(k) Next kLabel1 = 100, 1000内所有素数与为 : & SumEnd Sub34、随机产生 10 个 1100的正整数放入数组 , 显示产生的数 , 求最大值、最小值、平均值。 Private Sub Form_Click()Dim a(1 To 10) As Integer, i%, maxa%, mina%, suma%For i = 1 To 10a(i) = Int(Rnd * 71 + 30)Next imina = a(1)maxa = a(1)suma = a(1)For i = 2 To 10If a(i) maxa Then maxa = a(i)If a(i) mina Then mina = a(i)suma = suma + a(i)精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 20 页,共 27 页 - - - - - - - - - - VB编程程序代码Next iFor i = 1 To 10Print a(i);Next iPrintPrint max=; maxa, min=; mina; aver=; suma / 10End Sub35、已知一维数组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)n = UBound(a)x = Val(Text1)For k = 0 To nIf x a(k) Then Exit ForNext kReDim Preserve a(n + 1)For i = n To k Step -1 a(i + 1) = a(i)Next ia(k) = xFor i = 0 To n + 1 Print a(i); Next iEnd Sub37、声明一个整型的二维数组a(1 to 4,1 to 4), 用随机函数产生各元素, 范围介于 1,20之间, 编程将第 2 行与第 4 行对应元素交换。Private Sub Form_Click()精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 21 页,共 27 页 - - - - - - - - - - VB编程程序代码Dim a(0 To 3, 0 To 3), i%, j%, tFor i = 0 To 3 For j = 0 To 3 a(i, j) = Int(Rnd * 21 + 1) Print Tab(j * 5); a(i, j) Next jNext iPrintPrintFor j = 0 To 3t = a(1, j): a(1, j) = a(3, j): a(3, j) = tNext jFor i = 0 To 3 For j = 0 To 3Print Tab(j * 5); a(i, j) Next jNext iEnd Sub38、随机产生 10 个30,100内的整数 , 求最小值及所对应的下标。 Private Sub Form_Click()Dim a(1 To 10) As Integer, i%, mina%, m%For i = 1 To 10 a(i) = Int(Rnd * 70) + 30Next imina = a(1)For i = 2 To 10 If a(i) a(1) Then mina = a(i): m = i ElseIf a(i) = a(1) Then精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 22 页,共 27 页 - - - - - - - - - - VB编程程序代码 mina = a(i): m = i End IfNext iFor i = 1 To 10 Print a(i);Next i Print mina= & mina; 下标为 & mEnd Sub39、编一个判断宿舍的通用过程(函数或子过程 )。调用该过程 , 计算并输出区间 (1000, ,1100)内所有素数的与。Function ss(n) As IntegerDim i%, tag As Booleantag = TrueFor i = 3 To n / 2If (n Mod i) = 0 Then tag = FalseNext iIf tag Then ss = nEnd FunctionPrivate Sub Picture1_Click()Dim i%, averaavera = 0For i = 1000 To 1100If ss(i) 0 Then Print ss(i)avera = avera + ss(i)Next iPicture1、Print averaEnd Sub40、编一子过程 ProcMin,求一维数组 a中的最小值 , 子过程的形参自己确定。精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 23 页,共 27 页 - - - - - - - - - - VB编程程序代码Private Sub Form_Click() Dim a(1 To 10), amin, i% For i = 1 To 10 a(i) = Int(Rnd * 101) Print a(i); Next i Call ProcMin(a(), amin) Print Print amin =; aminEnd SubSub ProcMin(b(), min)Dim i%min = b(LBound(b)For i = LBound(b) + 1 To UBound(b) If b(i) min Then min = b(i) Next iEnd Sub41、编一个冒泡排序法子过程, 对已知的若干整数按递减次序排列。提示: 子过程的形式为 sub sort1提示: 子过程的形式为 sub sort1(a%()。Private Sub Command1_Click()Dim b%(10), i%Print 排序前 For i = 0 To 9b(i) = Int(Rnd * 100)Print b(i);Next iPrintPrint 排序后 Call sort(b()精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 24 页,共 27 页 - - - - - - - - - - VB编程程序代码End SubSub sort(a%()Dim i%, m%, n%, min%, j%n = UBound(a)For i = 0 To n - 1 For j = 0 To n - 1 - i If a(j) a(j + 1) Then m = a(j): a(j) = a(j + 1): a(j + 1) = m Next jNext iFor i = LBound(a) To UBound(a) - 1 Print a(i);Next iEnd Sub42、编一个选择排序法子过程,对已知的若干整数按递增次序排列。提示:子过程的形式为 sub sort1Private Sub Command1_Click()Dim b%(10), i%Print 排序前 For i = 1 To 10b(i) = Int(Rnd * 100)Print b(i);Next iPrintPrint 排序后 Call sort(b()End SubSub sort(a%()Dim i%, m%, n%, min%, t%n = UBound(a)精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 25 页,共 27 页 - - - - - - - - - - VB编程程序代码For i = 0 To n - 1 min = i For j = i + 1 To n If a(j) a(j + 1) Then m = a(j): a(j) = a(j + 1): a(j + 1) = m精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 26 页,共 27 页 - - - - - - - - - - VB编程程序代码 Next jNext iFor i = LBound(a) + 1 To UBound(a) Print a(i);Next iEnd Sub精品资料 - - - 欢迎下载 - - - - - - - - - - - 欢迎下载 名师归纳 - - - - - - - - - -第 27 页,共 27 页 - - - - - - - - - -