《最新VB程序设计编程考试题及答案.doc》由会员分享,可在线阅读,更多相关《最新VB程序设计编程考试题及答案.doc(22页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精品资料VB程序设计编程考试题及答案.VB程序设计编程考试题及答案1. 输入3个数,输出最大的数Private Sub command1_click()Dim maxFor i = 1 To 3n = Val(InputBox(输入第 & i & 个数)If n max Thenmax = nEnd IfNextPrint 三个数中最大的数是 ; maxEnd Sub2. 有一元二次方程:aX2+bX+c=0,依次输入a,b,c,判断此方程是否有实根Private Sub Command1_Click()Dim a, b, c, na = Text1b = Text2c = Text3n =
2、b * b - 4 * a * cIf n = 0 ThenLabel4 = 方程有根 Else Label4 = 方程无根End IfEnd Sub3. 输入一个百分制的成绩,若大于等于60,则用消息框弹出,显示“恭喜!成绩通过!”,若小于60,则弹出消息框,显示“抱歉!成绩没有通过!Private Sub Form_Load()Text1 = End SubPrivate Sub Command1_Click()Dim nn = Text1If n =60 ThenVal (MsgBox(恭喜!成绩通过!)ElseVal (MsgBox(抱歉!成绩没有通过!)End IfEnd Sub4.
3、 输入一年份,判断它是否为闰年,并显示有关信息。(判断闰年的条件是:年份能被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 ThenVal (MsgBox(闰年!)ElseVal (MsgBox(平年!)End IfEnd Sub5. 输入学生的百分制成绩,转换输出为五级制成绩,90100分转换为优秀,8089转换为良好,7079转换为中等,6069转换为及格,060转换为不及格。Private
4、Sub Command1_Click()a = Val(InputBox(请输入成绩)If a 100 Then Print 超出范围If a = 90 And a = 80 And a = 70 And a = 60 And a 70 Then Print 及格If a n1 Then m = m1: n = n1 Else m = n1: n = m1 End If r = m Mod n Do While r 0 m = n n = r r = m Mod n LoopPrint m n 的最大公约数是:; nEnd Sub8. 编程求200-400范围内5的倍数或7的倍数之和。(一个数
5、如果同时是7和5的倍数,则只能加一次。Private Sub Command1_Click()For i = 200 To 400If i Mod 5 = 0 Or i Mod 7 = 0 ThenSum = Sum + iEnd IfNext iFor i = 200 To 400If i Mod 5 = 0 And i Mod 7 = 0 ThenSum = Sum - iEnd IfNext iPrint “倍数之和=”; SumEnd Sub9. 计算100300之间所有能被3和7整除的数之和Private Sub Command1_Click()Dim iSum = 0For i =
6、 100 To 300If i Mod 3 = 0 And i Mod 7 = 0 ThenSum = Sum + iEnd IfNext iPrint SumEnd Sub10. 分别统计1-100中,满足3的倍数、7的倍数的数各有多少。Private Sub Command1_Click()Dim i, s3, s7s3 = 0s7 = 0For i = 1 To 100If i Mod 3 = 0 Thens3 = s3 + 1End IfNext iFor i = 1 To 100If i Mod 7 = 0 Thens7 = s7 + 1End IfNext iPrint 3的倍数;
7、 s3Print 7的倍数; s7End Sub11. 某次歌曲大奖赛,有7个评委。如果分别输入7个评委对某个参赛者的打分数,按照去掉一个最高分和一个最低分的计算办法,求出该参赛者的平均得分。Private Sub Command1_Click()Dim max, a(1 To 7), min, sFor i = 1 To 7a(i) = Val(InputBox(输入第 & i & 个数)Next imax = a(1)min = a(1)s = a(1)For i = 2 To 7If a(i) max Then max = a(i)If a(i) min Then min = a(i)s
8、 = s + a(i)Next is = s - min - maxs = s / 5Print 平均分; sEnd Sub12. 输入一串字符,统计其中字母A(不区分大小写)出现的次数。Private Sub Command1_Click()Dim c As String, a As Integerb = InputBox(请输入字符)For i = 1 To Len(b) c = UCase(Mid(b, i, 1) If c = A Then a = a + 1 End If Next i Print a出现的次数:; aEnd Sub13. 我国目前有13亿人口,按人口年增长率0.8%
9、计算,多少年后我国人口将超过26亿。Private Sub Command1_Click()Dim a, ia = 13i = 0Do While a =2 )Private Sub Command1_Click()Dim F(11), i As LongF(0) = 1F(1) = 1For i = 2 To 10F(i) = F(i - 1) + F(i - 2)Next iFor i = 0 To 11Print F(i);Next iEnd Sub18. 将输入的字符串以反序显示。例如:输入“ASDFGT”,显示“TGFDSA”Private Sub Command1_Click()D
10、im a, b, c, d, na = InputBox(输入字符串)n = Len(a)For b = 1 To Int(n 2)c = Mid(a, b, 1)Mid(a, b, 1) = Mid(a, n - b + 1, 1) Mid(a, n - b + 1, 1) = cNext bPrint aEnd Sub19. 随机产生10个30,100内的整数,求最大值及所对应的下标Private Sub Command1_Click()Dim a(1 To 10), mRandomizeFor I = 1 To 10a(I) = Int(Rnd * 71) + 30Print a(I)N
11、ext IMax = a(1)For I = 2 To 10If a(I) Max Then Max = a(I): m = INext IPrint max=; MaxPrint max的下标为; mEnd Sub20. 随机产生10个30,100内的整数,求最大值、最小值和平均值。Private Sub Command1_Click()Dim a(1 To 10)For i = 1 To 10a(i) = Int(Rnd * 71) + 30Print a(i)Next iMax = a(1)Min = a(1)Avg = a(1)For i = 2 To 10If a(i) Max Th
12、en Max = a(i)If a(i) Min Then Min = a(i) Avg = Avg + a(i)Next iAvg = Avg / 10Print max=; MaxPrint min=; MinPrint avg=; AvgEnd Sub 21. 随机产生10个30,100内的整数,求最小值及所对应的下标。Private Sub Command1_Click()Dim a(1 To 10), mRandomizeFor I = 1 To 10a(I) = Int(Rnd * 71) + 30Print a(I)Next IMin = a(1)For I = 2 To 10I
13、f a(I) Max Then Max = a(i)If a(i) Min Then Min = a(i) Avg = Avg + a(i)Next iAvg = Avg / 10Print max=; MaxPrint min=; MinPrint avg=; AvgEnd Sub24. 利用随机函数生成一个44的矩阵(即二维矩阵),范围是20,50内的整数,输出该矩阵所有数据之和Private Sub Command1_Click()Dim a%(4, 5), sumFor i = 1 To 4For j = 1 To 4a(i, j) = Int(Rnd * 31) + 20Print
14、a(i, j);sum = sum + a(i, j)Next jPrint 换行Next iPrint sum=; sumEnd Sub25. 已知有序数组a(),编程插入x(x的值为14)。数组a中的元素分别为12,6,4,89,75,63,100,20,31。Private Sub Command1_Click()Dim a(), i%, k%, x%, n%a = Array(4, 6, 12, 20, 31, 63, 75, 89, 100)n = UBound(a)x = Val(InputBox(请输入所要插入数)For k = 0 To nIf x a(k) Then Exit
15、 ForNext kReDim Preserve a(n + 1)For i = n To k Step -1a(i + 1) = a(i)Next ia(k) = xFor i = 0 To n + 1Print a(i);Next iEnd Sub26. 编写一函数,计算Double类型一维数组所有元素的平均值。27. 编一子过程ProcMin,求一维数组a中的最小值,子过程的形参自己确定Private Sub command1_Click()Dim a(1 To 10), min, i%For i = 1 To 10a(i) = Int(Rnd * 101)Print a(i);Next
16、 iCall ProcMin(a(), min)PrintPrint min =; minEnd Sub Sub 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 Sub28. 编一子过程ProcMin,求一维数组a中的最小值,子过程的形参自己确定。Private Sub Form_Click()Dim a(1 To 10), amin, i%For i = 1 To 10a(i) = Int(Rnd * 101)Print a(i);Next iCall ProcMin(a(), amin)PrintPrint amin =; aminEnd SubSub ProcMin(b(), min)Dim i%min = b(LBound(b)For i = LBound(b) + 1 To UBound(b)If b(i) max Then max = b(i)Next iEnd Sub30. 编写一个过程计算并输出区间100,1000内所有素数的和(要求判断素数使用过程(函数或子过程)来实现。调用该过程。(提示:只能被1和自身整除的自然数称为素数。)