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 = b * b - 4 * a * cIf n >= 0 ThenLabel4 = "方程有根"ElseLabel4 = "方程无根"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.输入一年份,判断它是否为闰年,并显示有关信息。
(判断闰年的条件是:年份能被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.输入学生的百分制成绩,转换输出为五级制成绩,90~100分转换为优秀,80~89转换为良好,70~79转换为中等,60~69转换为及格,0~60转换为不及格。
Private Sub Command1_Click()a = Val(InputBox("请输入成绩"))If a > 100 Then Print "超出范围"If a >= 90 And a <= 100 Then Print "优秀"If a >= 80 And a < 90 Then Print "良好"If a >= 70 And a < 80 Then Print "中等"If a >= 60 And a < 70 Then Print "及格"If a < 60 Then Print "不及格"End Sub6.随机产生一个三位正整数,然后逆序输出,产生的数与逆序数同时显示。
例如,产生246,输出是642。
Private Sub Command1_Click()Dim x%, x1%, x2%, x3%x = Text1x1 = x Mod 10x2 = (x Mod 100) \ 10x3 = x \ 100Label1 = x1 * 100 + x2 * 10 + x3End Sub7.输入两个整数,求它们的最大公约数。
private Sub Command1_Click()n1 = InputBox("输入n")m1 = InputBox("输入m")If m1 > n1 Thenm = m1: n = n1Elsem = n1: n = m1End Ifr = m Mod nDo While r <> 0m = nn = rr = m Mod nLoopPrint "m n 的最大公约数是:"; n8.编程求200--400范围内5的倍数或7的倍数之和。
(一个数如果同时是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.计算100~300之间所有能被3和7整除的数之和Private Sub Command1_Click()Dim iSum = 0For i = 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的倍数"; s3Print "7的倍数"; s711.某次歌曲大奖赛,有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 = 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" Thena = a + 1End IfNext iPrint "a出现的次数:"; aEnd Sub13.我国目前有13亿人口,按人口年增长率0.8%计算,多少年后我国人口将超过26亿。
Private Sub Command1_Click()Dim a, ia = 13i = 0Do While a < 26a = a * (1.008)i = i + 1LoopPrint iEnd Sub14.小猴有桃若干,第一天吃掉一半多一个;第二天吃剩下桃子的一半多一个;以后每天都吃尚存桃子的一半多一个,到第7天只剩一个,问小猴原有桃多少?Private Sub Command1_Click()Dim a, ia = 1For i = 1 To 6a = 2 * (a + 1)Next iPrint "小猴原有桃"; a;”只”End Sub15.求100以内的素数Private Sub Command1_Click()Dim i, jFor i = 2 To 100For j = 2 To i - 1If i Mod j = 0 Then Exit ForNext jIf j = i Then Print iNext iEnd Sub16.百元买百鸡问题。
假定小鸡每只5角,公鸡每只2元,母鸡每只3元,编程求解购鸡方案。
Private Sub Command1_Click()Print "小鸡"; " "; "母鸡"; " "; "公鸡"Print ""For x = 1 To 100For y = 1 To 100For z = 1 To 100If 0.5 * x + 2 * y + 3 * z = 100 ThenPrint ""; x; " "; y; " "; zEnd IfNext zNext yNext xEnd Sub17.编程求斐波那契数列前11项。
(斐波那契数列:F(0)=f(1)=1, F(n)=F(n-1)+F(n-2) n>=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()Dim 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)Next 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]内的整数,求最大值、最小值和平均值。