VB编程题代码43例.doc
《VB编程题代码43例.doc》由会员分享,可在线阅读,更多相关《VB编程题代码43例.doc(80页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateVB编程题代码43例VB编程题代码43例VB编程题代码43例1、求100以内的素数。 Private Sub Form_Click()Dim i%, j%For i = 2 To 100For j = 2 To i - 1If i Mod j = 0 Then Exit ForNext jIf j = i Then Print iNext iEnd Sub2、从键盘输
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) = cNext IPrint aEnd Sub3、计算0200之间所有能被11或5整除的数之和 Private Sub Form_Click()Dim
3、n%, i%n = 0For i = 1 To 200If i Mod 11 = 0 Or i Mod 5 = 0 Thenn = n + iEnd IfNext iPrint 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 ThenMsgBox (y & 年是闰年) ElseMsgBox (y &
4、年是平年) 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; ; z End Sub6、求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。 Priva
5、te Sub Form_Click()Dim s!, t!, i!, a%, n%a = Int(Rnd * 9 + 1)n = Int(Rnd * 6 + 5)t = 0: s = 0Print a=; a, n=; nFor i = 1 To nt = t * 10 + as = s + tPrint t;Next iPrintPrint s=; sEnd Sub7、计算100300之间所有能被3和7整除的数之和。 Private Sub Form_Click()For i = 100 To 300If i Mod 21 = 0 Thens = s + iPrint i;End IfNex
6、t iPrintPrint s=; sEnd Sub8、编程求200-400范围内5的倍数或7的倍数之和。(一个数如果同时是7和5的倍数,则只能加一次。) Private Sub Command1_Click()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
7、除,余数为1的最小的5个正整数。 Private Sub Command1_Click()Dim CountN%, n%CountN = 0n = 1Don = n + 1If n Mod 3 = 1 And n Mod 5 = 1 And n Mod 7 = 1 ThenPrint nCountN = CountN + 1End IfLoop Until CountN = 5End Sub10、某次歌曲大奖赛,有7个评委。如果分别输入7个评委对某个参赛者的打分数,按照去掉一个最高分和一个最低分的计算办法,求出该参赛者的平均得分。 Private Sub Command1_Click()Dim
8、 mark!, max!, min!, aver!, i%aver = 0For i = 1 To 7mark = InputBox(输入第 & i & 位评委的打分) If i = 1 Thenmax = mark: min = markElseIf mark max Then max = markEnd Ifaver = aver + markNext iaver = (aver - min - max) / 5Print averEnd Sub11、编程显示100500之间所有的水仙花数之和。(水仙花数是3位数,其各位数之和等于该数本身) Private Sub Form_Click()
9、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 + aEnd IfNext iPrint aEnd Sub12、随机产生一个三位正整数,然后逆序输出,产生的数与逆序数同时显示。例如,产生246,输出是642。 Dim a%, m%, b%, c%, d%Private Sub Form_Click()a = Int(Rnd * 900 + 100)Print ad =
10、(a Mod 10) * 100b = (a Mod 100) - (a Mod 10)c = a 100m = c + b + dPrint 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 ThenMsgBox (能构成三角形) ElseMsgBox (
11、不能构成三角形) End IfEnd Sub14、已知数组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 nPrint a(i);Next iPrintFor i = 5 To na(i - 1) = a(i)Next in = n - 1ReDim Preserve a(n)For i = 0 To nPrint a(i
12、);Next iPrintEnd Sub15、随机生成一个整型的二维数组,范围在10,20之间,以上三角形式输出该数组。(下三角、全部元素)Dim a%(4, 4) Dim a%(4, 4)Private Sub Command1_Click()Picture1.ClsFor i = 0 To 4For j = i To 4Picture1.Print Tab(j * 6); a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Form_Load()For i = 0 To 4For j = 0 To 4a(i, j) = Int(Rnd
13、* 11 + 10)Next jNext iEnd Sub16、利用随机函数生成一个44的矩阵(即二维矩阵),范围是20,50内的整数,输出每行中的最大值和下标。 Private Sub Form_Click()Dim a%(3, 3), s0%, s1%, s2%, s3%, b0%, b1%, b2%, b3%Max = 40For i = 0 To 3For 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)
14、 = 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内的整数,求它的最大值及所对应的下标
15、。 Private Sub Form_Click()Dim a%(3, 3)Min = 80For i = 0 To 3For 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 最小值为; Min Print 其下标为; (; b, c; ) End Sub18、输入一系列字符串,编程求出长度最大的字符串并输出其所对应的次序。Private Sub Form_Cli
16、ck()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 iPrint max=;a(i)Print cixu=,imax+1End sub19、利用随机函数生成一个44的矩阵(即二维矩阵),范围是20,50内的整数,输出该矩阵所有数据之和 Private Sub Form_Click()Dim a%
17、(3, 3)For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 31 + 20)Print Tab(j * 5); a(i, j);Next jNext is = 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
18、(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 nFor j = 1 To n - iIf a(j) amax Then amax = a(i)If a(i) a(1) Thenmaxa = a(i): m = iEnd IfNext iFor i = 1 To 10Print a(i);Next iPrint maxa= & maxa;
19、 下标为 & m End 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 3For j = 0 To 3a(i, j) = Int(Rnd * 20 + 1)Picture1.Print a(i, j);Next jNext iPicture1.PrintFor i = 0 To 3For j = 0 To 3b(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VB 编程 代码 43
限制150内