VB程序设计编程考试题及答案
1. 输入3个数,输出最大的数
Private Sub command1_click() Dim max For i = 1 To 3
n = Val(InputBox(\输入第\个数\If n > max Then max = n End If Next
Print \三个数中最大的数是\End Sub
2. 有一元二次方程:aX2+bX+c=0,依次输入a,b,c,判断此方程是否有实根
Private Sub Command1_Click() Dim a, b, c, n a = Text1 b = Text2 c = Text3
n = b * b - 4 * a * c If n >= 0 Then
Label4 = \方程有根\ Else
Label4 = \方程无根\End If End Sub
3. 输入一个百分制的成绩,若大于等于60,则用消息框弹出,显示“恭喜!成绩通过!”,
若小于60,则弹出消息框,显示“抱歉!成绩没有通过! Private Sub Form_Load() Text1 = \End Sub
Private Sub Command1_Click() Dim n n = Text1
If n > =60 Then
Val (MsgBox(\恭喜!成绩通过!\Else
Val (MsgBox(\抱歉!成绩没有通过!\End If 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 Val (MsgBox(\闰年!\Else
Val (MsgBox(\平年!\End If End Sub
5. 输入学生的百分制成绩,转换输出为五级制成绩,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 Sub
6. 随机产生一个三位正整数,然后逆序输出,产生的数与逆序数同时显示。例如,产生246,
输出是642。
Private Sub Command1_Click() Dim x%, x1%, x2%, x3% x = Text1
x1 = x Mod 10
x2 = (x Mod 100) \\ 10 x3 = x \\ 100
Label1 = x1 * 100 + x2 * 10 + x3 End Sub
7. 输入两个整数,求它们的最大公约数。
private Sub Command1_Click() n1 = InputBox(\输入n\ m1 = InputBox(\输入m\ If m1 > 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 Loop
Print \的最大公约数是:\End Sub
8. 编程求200--400范围内5的倍数或7的倍数之和。(一个数如果同时是7和5的倍数,则
只能加一次。。
Private Sub Command1_Click() For i = 200 To 400
If i Mod 5 = 0 Or i Mod 7 = 0 Then Sum = Sum + i End If Next i
For i = 200 To 400
If i Mod 5 = 0 And i Mod 7 = 0 Then Sum = Sum - i End If Next i
Print “倍数之和=”; Sum End Sub
9. 计算100~300之间所有能被3和7整除的数之和
Private Sub Command1_Click() Dim i Sum = 0
For i = 100 To 300
If i Mod 3 = 0 And i Mod 7 = 0 Then Sum = Sum + i End If Next i Print Sum End Sub
10. 分别统计1-100中,满足3的倍数、7的倍数的数各有多少。
Private Sub Command1_Click() Dim i, s3, s7 s3 = 0 s7 = 0
For i = 1 To 100
If i Mod 3 = 0 Then s3 = s3 + 1 End If Next i
For i = 1 To 100 If i Mod 7 = 0 Then s7 = s7 + 1 End If Next i
Print \的倍数\Print \的倍数\End Sub
11. 某次歌曲大奖赛,有7个评委。如果分别输入7个评委对某个参赛者的打分数,按照去
掉一个最高分和一个最低分的计算办法,求出该参赛者的平均得分。 Private Sub Command1_Click() Dim max, a(1 To 7), min, s For i = 1 To 7
a(i) = Val(InputBox(\输入第\个数\Next i max = a(1) min = a(1) s = a(1)
For i = 2 To 7
If a(i) > max Then max = a(i) If a(i) < min Then min = a(i) s = s + a(i) Next i
s = s - min - max s = s / 5
Print \平均分\End Sub
12. 输入一串字符,统计其中字母A(不区分大小写)出现的次数。
Private Sub Command1_Click() Dim c As String, a As Integer b = InputBox(\请输入字符\For i = 1 To Len(b)
c = UCase(Mid(b, i, 1)) If c = \ a = a + 1 End If Next i
Print \出现的次数:\End Sub
13. 我国目前有13亿人口,按人口年增长率0.8%计算,多少年后我国人口将超过26亿。
Private Sub Command1_Click() Dim a, i a = 13 i = 0
Do While a < 26 a = a * (1.008) i = i + 1 Loop Print i End Sub
14. 小猴有桃若干,第一天吃掉一半多一个;第二天吃剩下桃子的一半多一个;以后每天都
吃尚存桃子的一半多一个,到第7天只剩一个,问小猴原有桃多少?
Private Sub Command1_Click() Dim a, i a = 1
For i = 1 To 6 a = 2 * (a + 1) Next i
Print \小猴原有桃\”只” End Sub
15. 求100以内的素数
Private Sub Command1_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
16. 百元买百鸡问题。假定小鸡每只5角,公鸡每只2元,母鸡每只3元,编程求解购鸡方
案。
Private Sub Command1_Click()
Print \小鸡\ \母鸡\ \公鸡\Print \
For x = 1 To 100 For y = 1 To 100 For z = 1 To 100
If 0.5 * x + 2 * y + 3 * z = 100 Then Print \ \ \End If