1. 编程在窗体上输出101~500之间的所有能被3或者7整除的奇数,按照一行5个的方式输出并最后符合要求的数的和。
解:代码为
Private Sub form_load()
Show
Dim n, sum As Integer
Print Spc(6); "101~500之间的所有能被3或者7整除的奇数,及各数的和"
i = 0
sum = 0
For n = 101 To 500 Step 2
If n Mod 3 = 0 Or n Mod 7 = 0 Then
sum = sum + n
i = i + 1
Print Spc(6); n;
If i Mod 5 = 0 Then
Print
End If
End If
Next n
Print Spc(7); "sum="; sum
End Sub
2. 在4个TextBox中输入4个大小不同的数,将它们从大到小排序后的结果用Msgbox函数输出。
解: 代码为
Private Sub Command1_Click()
Dim a!, b!, c!, d!
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
d = Val(Text4.Text)
If a <> b And b <> c And c <> d And a <> c And a <> d And b <> d Then
If a < b Then
t = a: a = b: b = t
End If
If a < c Then
t = a: a = c: c = t
End If
If b < c Then
t = b: b = c: c = t
End If
If d > a Then
t = d: d = a: a = t
End If
If d > b Then
t = d: d = b: b = t
End If
If d > c Then
t = d: d = c: c = t
End If
MsgBox "从大到小排序:" & a & "," & b & "," & c & "," & d
Else
MsgBox "有相同的数"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
3. 在TextBox任意输入x的值,将f(x)输出的结果用Msgbox函数输出。
解: 代码为
Private Sub Command1_Click()
Dim x As Double
x = Val(Text1.Text)
If x < 0 Then
If x = -3 Then
MsgBox "f(x)=" & x ^ 2 - x - 1
Else: MsgBox "f(x)=" & x ^ 2 + x - 6
End If
End If
If x >= 0 Then
If x < 10 And x <> 2 And x <> 3 Then
MsgBox "f(x)=" & x ^ 2 - 5 * x + 6
Else: MsgBox "f(x)=" & x ^ 2 - x - 1
End If
End If
End Sub
Private Sub Command2_Click()
End
End Sub
4. 找到10000到99999中的所有回文数,并按照一行10个的格式在窗体上输出,例如12321是回文数,个位与万位相同,十位与千位相同。
解:代码为
Private Sub Form_Load()
Show
Dim x, a, b, c, d, e As Integer
Print Spc(25); "所有的五位回文数:"
Print
i = 0
For x = 10000 To 99999
a = x \ 10000
b = (x - (a * 10000)) \ 1000
c = (x - (a * 10000) - (b * 1000)) \ 100
d = (x - (a * 10000) - (b * 1000) - (c * 100)) \ 10
e = x - (a * 10000) - (b * 1000) - (c * 100) - (d * 10)
If a = e And b = d And c <> a And c <> b And a <> b Then
Print Spc(5); x;
i = i + 1
If i Mod 10 = 0 Then
Print
End If
End If
Next
End Sub
5. 在窗体上输出所有的“水仙花数”,并按照一行5个的格式输出。所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1*1*1+5*5*5+3*3*3。
解:代码为
Private Sub form_load() Show Dim a, b, c, i As Integer Print Spc(10); "水仙花数" For i = 100 To 999 a = i Mod 10 b = i \ 100 c = (i - b * 100) \ 10 If a ^ 3 + b ^ 3 + c ^ 3 = i Then Print i; Space(2); If (i + 1) Mod 5 = 0 Then Print End If End If Next i End Sub
6. 编程找出满足下列条件的所有四位数并在窗体上按照一行10个的形式输出:该数第一、三位数字之和为10,第二、四位数字之积为12。
解:代码为
Private Sub Form_Load()
Dim i As Integer, a(1 To 4) As Integer, j As Integer
j = 0
Show
For i = 1000 To 9999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
If (a(4) + a(2) = 10 And a(1) * a(3) = 12) Then
Print Tab((j Mod 10 + 1) * 7);
Print i;
j = j + 1
If (j Mod 10 = 0) Then Print
End If
Next
End Sub
7.编写一个程序,在2个TextBox中输入某人的身高(cm)和体重(kg),按下式确定其体重是否为标准、过胖或过瘦,将结果用Msgbox输出。(1)标准体重=身高-110;(2)超过标准体重5kg为过胖;(3)低于标准体重5kg为过瘦。
解: 代码为
Private Sub Command1_Click()
Dim a, b, c As Single
a = Val(Text1.Text)
b = Val(Text2.Text)
c = a - 110
If b - c > 5 Then
MsgBox "体重过胖"
ElseIf c - b > 5 Then
MsgBox "体重过痩"
ElseIf c - b <> 5 Or c - b = 5 Then
MsgBox "标准体重"
End If
End Sub
Private Sub Command2_Click()
End
End Sub
8.在TextBox中输入一个正整数n,计算 的值并在窗体上输出结果。
解: 代码为
Private Sub Command1_Click()
Dim n, f, s, i As Integer
n = Val(Text1.Text)
f = 1
s = 0
For i = 1 To n
f = f * i
s = s + f / (i + 2)
Next i
Label2.Caption = "S=" & s
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
End Sub
9.已知abcd+dcba=3456,其中a,b,c,d均为一位正整数,编程求出满足条件的a,b,c,d所有组合,在Label上输出答案。
解: 代码为
Private Sub Command1_Click()
Dim a, b, c, d, i As Integer
i = 0
For a = 0 To 9
For b = 0 To 9
For c = 0 To 9
For d = 0 To 9
If (a * 1000 + b * 100 + c * 10 + d + d * 1000 + c * 100 + b * 10 + a = 3456) Then
Label1.Caption = "a=" & a & "," & "b=" & b & "," & "c=" & c & "," & "d=" & d
i = i + 1
Else
Label1.Caption = "没有这样的数"
End If
Next
Next
Next
Next
End Sub
10.编程输出所有满足如下条件的三位正整数:它是某整数的平方,它的三位数码有两位是相同的,在窗体上按照一行5个的格式输出。(如100是10的平方,它有两个0,225是15的平方,它有两个2)。
解:代码为
Private Sub Form_Load()
Show
Dim x, a, b, c, m As Integer
Print Tab(10); "所有的三位数:"
Print
i = 0
For x = 100 To 999
For m = 10 To 32
a = x \ 100
b = (x - (a * 100)) \ 10
c = x - (a * 100) - (b * 10)
If a = b Or b = c Or c = a Then
If x = m * m Then
Print Spc(5); x;
i = i + 1
If i Mod 5 = 0 Then
Print
End If
End If
End If
Next m
Next x
End Sub
11.求这样一个四位数并输出该数字,该四位数等于其每位数字的阶乘之和。即:
abcd = a! + b! + c! + d!,将结果用Msgbox函数输出。
解: 代码为
Private Sub Command1_Click()
Dim i As Integer, a(1 To 4) As Integer, j As Integer
j = 0
For i = 1000 To 9999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
If (i = fact(a(1)) + fact(a(2)) + fact(a(3)) + fact(a(4))) Then
MsgBox ("其中一个四位数为:" & i)
j = j + 1
End If
Next
If j = 0 Then MsgBox ("无结果")
End Sub
Private Function fact(n As Integer) As Long
Dim i As Integer
f = 1
For i = 1 To n
f = f * i
Next
fact = f
End Function
12.输出所有大于1010的4位偶数,且该类偶数的各位数字两两不相同,在窗体上按照一行5个的格式输出。
解:代码为
Private Sub form_load()
Show
Dim a, b, c, d As Integer
Print Tab(20); "所有的四位数:"
i = 0
For x = 1010 To 9999 Step 2
a = x \ 1000
b = (x - (a * 1000)) \ 100
c = (x - (a * 1000) - (b * 100)) \ 10
d = x - (a * 1000) - (b * 100) - (c * 10)
If a <> b And b <> c And c <> a And a <> d And b <> d And c <> d Then
Print Spc(5); x;
i = i + 1
If i Mod 5 = 0 Then
Print
End If
End If
Next
End Sub
13.求1-2!+3!-4!...序列,当最后的结果超过2000000停止计算,将该序列的最结果用Msgbox函数输出。
解: 代码为
Private Function m(n As Double) As Double
Dim i As Double
f = 1
For i = 1 To n
f = f * i
Next
m = f
End Function
Private Sub Command1_Click()
Dim i As Integer, b As Double, j As Double
b = 0
i = 1
j = 1
Do While (b < 2000000)
b = b + i * m(j)
j = j + 1
i = i * (-1)
Loop
MsgBox ("结果为:" & b)
End Sub
14.一位司机酒驾撞人逃跑。现场三人目击事件,但都没记住车号,只记下车号的一些特征。甲说:牌照的前两位数字是相同的;乙说:牌照的后两位数字是相同的;丙是位数学家,他说:四位的车号刚好是一个整数的平方。请根据以上线索求出车号。(车号为4位正整数,结果在TextBox中输出)
解: 代码为
Private Sub Form_Load()
Dim i As Integer, j As Integer, a(1 To 4) As Integer
j = 0
Show
For i = 1000 To 9999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
If (Sqr(i) - Fix(Sqr(i)) = 0) Then
If (a(1) = a(2) And a(3) = a(4)) Then
Text1.Text = i
End If
End If
Next
End Sub
15.用户利用InputBox函数随机输入12个0-100之间的整数,统计出小于60,60到70,70到80,80到90以及90到100范围的整数各有多少个?用Msgbox函数显示输出结果。
解: 代码为
Private Sub Command1_Click()
Dim a(1 To 12) As Integer, b(1 To 5) As Integer, i As Integer
For i = 1 To 12
a(i) = Val(InputBox("请输入第" & i & "个数"))
Next
For i = 1 To 12
Select Case a(i)
Case 0 To 59
b(1) = b(1) + 1
Case 60 To 69
b(2) = b(2) + 1
Case 70 To 79
b(3) = b(3) + 1
Case 80 To 90
b(4) = b(4) + 1
Case 90 To 100
b(5) = b(5) + 1
Case Else
End Select
Next
MsgBox ("小于60:" & b(1) & ",60到70:" & b(2) & ",70到80:" & b(3) & ",80到90:" & b(4) & ",90到100:" & b(5))
End Sub
16.在TextBox中输入一个正整数n,当n的值为偶数时,求1-1/2-1/4-...-1/n序列的和,当n为奇数时,求1-1/3-1/5...-1/n序列的和,将结果用Msgbox函数输出。
解: 代码为
Private Sub Command1_Click()
Dim a As Integer, b As Double, x As Double
a = Val(Text1.Text)
x = 1
Select Case a Mod 2
Case 0
For b = 2 To a Step 2
x = x - 1 / b
Next
Case 1
For b = 3 To a Step 2
x = x - 1 / b
Next
Case Else
End Select
MsgBox ("结果为:" & Format(x, "0.#####"))
End Sub
17.编制程序要求输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上的数字,否则输出两数字之和。结果利用Msgbox函数输出 。
解: 代码为
Private Sub Command1_Click()
Dim a As Long, b As Long, s As Long, n As Long
a = Val(Text1.Text)
b = Val(Text2.Text)
s = a ^ 2 + b ^ 2
n = a + b
If s > 100 Then
i = MsgBox("结果为:" & Int(s / 100))
Else: i = MsgBox("结果为:" & n)
End If
End Sub
Private Sub Command2_Click()
End
End Sub
18.猜数游戏:由计算机随机产生一个2位正整数让人来猜,只能猜8次,如果人猜对了,则利用Msgbox函数显示计算机随机产生的该2位正整数并显示“You are so clever”,否则利用Msgbox函数给出提示,告诉人所猜的数是太大还是太小,直到人猜对为止,或者8次都猜不对给出提示“Game Over”。
解: 代码为
Private Sub Command1_Click()
Dim a As Integer, b As Integer, i As Integer
Randomize
a = Fix(Rnd(Time) * 90) + 10
For i = 1 To 8
b = Val(InputBox("请输入一个数"))
Select Case b
Case 0 To a - 1
MsgBox ("太小了!")
Case a + 1 To 100
MsgBox ("太大了!")
Case a
MsgBox ("You are so clever!")
Exit For
Case Else
MsgBox ("输入错误!")
End Select
If (i = 8) Then MsgBox ("GAME OVER!")
Next
End Sub
19.征兵的条件是:男性(sex)年龄(age)在18~20之间,身高(size)在1.65米以上;或者女性(sex)年龄(age)在16~18之间,身高(size)在1.60米以上。
用三个文本框控件分别输入性别、年龄、身高信息,根据这些信息判断是否符合征兵条件,用Msgbox函数输出结果。
解: 代码为
Private Sub Command1_Click()
Dim i As String, j As Single, k As Integer
i = Text1.Text
j = Val(Text2.Text)
k = Val(Text3.Text)
Show
Print i
Select Case i
Case "男"
If (k >= 18 And k <= 20) Then
If (j > 165) Then
MsgBox ("合格,符合征兵条件")
Else
MsgBox ("不合格,不符合征兵条件")
End If
Else
MsgBox ("不合格,不符合征兵条件")
End If
Case "女"
If (k >= 16 And k <= 18) Then
If (j > 160) Then
MsgBox ("合格,符合征兵条件")
Else
MsgBox ("不合格,不符合征兵条件")
End If
Else
MsgBox ("不合格,不符合征兵条件")
End If
Case Else
MsgBox ("输入错误!")
End Select
End Sub
Private Sub Command2_Click()
End
End Sub
20.求出1到1000之内能被5或13整除、但不能同时被5和13整除的所有整数,在窗体上按照一行5个的格式输出。
解:代码为
Private Sub Form_Load()
Show
Dim n As Integer
Print Spc(10); "1到1000之间能被5或13整除,但不能5和13同时整除的所有整数:"
i = 0
For n = 1 To 1000
If n Mod 5 = 0 Or n Mod 13 = 0 Then
If n Mod 65 <> 0 Then
i = i + 1
Print Spc(6); Format(n, "!@@@@");
If i Mod 5 = 0 Then
Print
End If
End If
End If
Next
End Sub
1. 在窗体上输出所有的“水仙花数”,并按照一行5个的格式输出。所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1*1*1+5*5*5+3*3*3。
解:代码为:
Private Sub Form_Load()
Dim i As Integer, j As Integer, g As Integer, b As Integer, q As Integer
Show
j = 0
For i = 100 To 999
g = i Mod 10
s = ((i - g) / 10) Mod 10
b = (i - s * 10 - g) / 100
If (i = g * g * g + s * s * s + b * b * b) Then
j = j + 1
Print Tab((j Mod 5 + 1) * 7); i;
If (j Mod 5 = 0) Then
Print ""
End If
End If
Next
End Sub
2.
在TextBox任意输入x的值,将f(x)输出的结果用Msgbox函数输出。
解:在窗体上创建一个文本框,和一个caption属性为“计算”的按钮command1
代码为:
Private Sub Command1_Click()
Dim a As Double, b As Double
a = Val(Text1.Text)
If (a < 0 And a <> -3) Then
b = a * a + a - 6
Else
If (a >= 0) Then
If (a < 10 And a <> 2 And a <> 3) Then
b = a * a - 5 * a + 6
Else
b = a * a - a - 1
End If
End If
End If
MsgBox ("计算结果为:" & b)
End Sub
3. 编写一个程序,在2个TextBox中输入某人的身高(cm)和体重(kg),按下式确定其体重是否为标准、过胖或过瘦,将结果用Msgbox输出。(1)标准体重=身高-110;(2)超过标准体重5kg为过胖;(3)低于标准体重5kg为过瘦。
解:代码为:
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
c = a - 110
If (b - c < -5) Then
MsgBox ("体重过瘦")
Else
If (b - c > 5) Then
MsgBox ("体重过肥")
Else
MsgBox ("体重很标准")
End If
End If
End Sub
4. 征兵的条件是:男性(sex)年龄(age)在18~20之间,身高(size)在1.65米以上;或者女性(sex)年龄(age)在16~18之间,身高(size)在1.60米以上。
用三个文本框控件分别输入性别、年龄、身高信息,根据这些信息判断是否符合征兵条件,用Msgbox函数输出结果。
解:窗体控件有三个标签caption属性分别为“性别”、“身高”、“年龄”
三个文本窗口
一个按钮caption属性为“判断是否符合条件”
代码为:
Private Sub Command1_Click()
Dim i As String, j As Integer, k As Integer
i = Text1.Text
j = Val(Text2.Text)
k = Val(Text3.Text)
Show
Print i
Select Case i
Case "男"
If (k > 18 And k < 20) Then
If (j > 165) Then
MsgBox ("合格,符合征兵条件")
Else
MsgBox ("不合格,不符合征兵条件")
End If
Else
MsgBox ("不合格,不符合征兵条件")
End If
Case "女"
If (k > 16 And k < 18) Then
If (j > 160) Then
MsgBox ("合格,符合征兵条件")
Else
MsgBox ("不合格,不符合征兵条件")
End If
Else
MsgBox ("不合格,不符合征兵条件")
End If
Case Else
MsgBox ("输入错误!")
End Select
End Sub
5. 编程在窗体上输出101~500之间的所有能被3或者7整除的奇数,按照一行5个的方式输出并最后符合要求的数的和。
解:代码为:
Private Sub Form_Load()
Dim i As Integer, j As Integer
j = 0
Show
For i = 101 To 500
If ((i Mod 3 = 0 or i Mod 7 = 0) And i Mod 2 <> 0) Then
Print Tab((j Mod 5 + 1) * 7);
Print i;
j = j + 1
If (j Mod 5 = 0) Then Print ""
End If
Next
End Sub
6. 在TextBox中输入一个正整数n,计算 的值并在窗体上输出结果。
解:控件有一个文本框,一个按钮caption属性为“计算”
代码为:
Private Function m(n As Double) As Double
Dim i As Double
f = 1
For i = 1 To n
f = f * i
Next
m = f
End Function
Private Sub Command1_Click()
Dim n As Integer, i As Double, x As Double
Show
n = Val(Text1.Text)
For i = 1 To n
x = x + m(i) / (i + 2)
Next
Print x
End Sub
7. 在4个TextBox中输入4个大小不同的数,将它们从大到小排序后的结果用Msgbox函数输出。
解:command1的caption属性为“排序”
代码为:
Private Sub Command1_Click()
Dim a(1 To 4) As Integer
Dim m As Integer, i As Integer, j As Integer
a(1) = Val(Text1.Text)
a(2) = Val(Text2.Text)
a(3) = Val(Text3.Text)
a(4) = Val(Text4.Text)
For i = 1 To 4
For j = i + 1 To 4
If (a(j) < a(i)) Then
m = a(j)
a(j) = a(i)
a(i) = m
End If
Next
Next
MsgBox ("排序结果为:" & a(1) & "," & a(2) & "," & a(3) & "," & a(4))
End Sub
8. 求1-2!+3!-4!...序列,当最后的结果超过2000000停止计算,将该序列的最结果用Msgbox函数输出。
解:代码为:
Private Function m(n As Double) As Double
Dim i As Double
f = 1
For i = 1 To n
f = f * i
Next
m = f
End Function
Private Sub Command1_Click()
Dim i As Integer, b As Double, j As Double
b = 0
i = 1
j = 1
Do While (b < 2000000)
b = b + i * m(j)
j = j + 1
i = i * (-1)
Loop
MsgBox ("结果为:" & b)
End Sub
9. 找到10000到99999中的所有回文数,并按照一行10个的格式在窗体上输出,例如12321是回文数,个位与万位相同,十位与千位相同。
解:代码为:
Private Sub Command1_Click()
Dim a(1 To 5) As Long
Dim i As Long, j As Long
For i = 10000 To 99999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
a(5) = ((i - i Mod 10000) / 10000) Mod 10
If (a(1) = a(5) And a(2) = a(4)) Then
Print Tab((j Mod 10 + 1) * 7);
Print i;
j = j + 1
If (j Mod 10 = 0) Then
Print ""
End If
End If
Next
End Sub
10. 求出1到1000之内能被5或13整除、但不能同时被5和13整除的所有整数,在窗体上按照一行5个的格式输出。
解:代码为:
Private Sub Form_Load()
Dim i As Integer, j As Integer
j = 0
Show
For i = 1 To 1000
If (i Mod 13 = 0 Or i Mod 5 = 0) Then
If (i Mod 13 = 0 And i Mod 5 = 0) Then
Else
Print Tab((j Mod 5 + 1) * 7);
Print i;
j = j + 1
If (j Mod 5 = 0) Then Print ""
End If
End If
Next
End Sub
11. 在TextBox中输入一个正整数n,当n的值为偶数时,求1-1/2-1/4-...-1/n序列的和,当n为奇数时,求1-1/3-1/5...-1/n序列的和,将结果用Msgbox函数输出。
解:代码为:
Private Sub Command1_Click()
Dim a As Integer, b As Double, x As Double
a = Val(Text1.Text)
x = 1
Select Case a Mod 2
Case 0
For b = 2 To a Step 2
x = x - 1 / b
Next
Case 1
For b = 3 To a Step 2
x = x - 1 / b
Next
Case Else
End Select
MsgBox ("结果为:" & Format(x, "0.00000000000"))
End Sub
12. 已知abcd+dcba=3456,其中a,b,c,d均为一位正整数,编程求出满足条件的a,b,c,d所有组合,在Label上输出答案。
解:Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer, d As Integer
Dim i As Integer
i = 0
For a = 0 To 9
For b = 0 To 9
For c = 0 To 9
For d = 0 To 9
If (a * 1000 + b * 100 + c * 10 + d + d * 1000 + c * 100 + b * 10 + a = 3456) Then
Label1.Caption = "a=" & a & "," & "b=" & b & "," & "c=" & c & "," & "d=" & d
i = i + 1
End If
Next
Next
Next
Next
If i = 0 Then MsgBox ("无结果!")
End Sub
13. 求这样一个四位数并输出该数字,该四位数等于其每位数字的阶乘之和。即:
abcd = a! + b! + c! + d!,将结果用Msgbox函数输出。
解:代码为:
Private Sub Command1_Click()
Dim i As Integer, a(1 To 4) As Integer, j As Integer
j = 0
For i = 1000 To 9999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
If (i = fact(a(1)) + fact(a(2)) + fact(a(3)) + fact(a(4))) Then
MsgBox ("其中一个四位数为:" & i)
j = j + 1
End If
Next
If j = 0 Then MsgBox ("无结果")
End Sub
Private Function fact(n As Integer) As Long
Dim i As Integer
f = 1
For i = 1 To n
f = f * i
Next
fact = f
End Function
14. 编程找出满足下列条件的所有四位数并在窗体上按照一行10个的形式输出:该数第一、三位数字之和为10,第二、四位数字之积为12。
解:代码为:
Private Sub Form_Load()
Dim i As Integer, a(1 To 4) As Integer, j As Integer
j = 0
Show
For i = 1000 To 9999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
If (a(4) + a(2) = 10 And a(1) + a(3) = 12) Then
Print Tab((j Mod 10 + 1) * 7);
Print i;
j = j + 1
If (j Mod 10 = 0) Then Print
End If
Next
End Sub
15. 编程输出所有满足如下条件的三位正整数:它是某整数的平方,它的三位数码有两位是相同的,在窗体上按照一行5个的格式输出。(如100是10的平方,它有两个0,225是15的平方,它有两个2)。
解:代码为:
Private Sub Form_Load()
Dim i As Integer, j As Integer, a(1 To 3) As Integer
j = 0
Show
For i = 100 To 999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
If (Sqr(i) - Fix(Sqr(i)) = 0) Then
If (a(1) = a(2) Or a(2) = a(3) Or a(1) = a(3)) Then
Print Tab((j Mod 5 + 1) * 7);
Print i;
j = j + 1
If (j Mod 5 = 0) Then Print
End If
End If
Next
End Sub
16. 输出所有大于1010的4位偶数,且该类偶数的各位数字两两不相同,在窗体上按照一行5个的格式输出。
解:代码为:
Private Sub Form_Load()
Dim i As Integer, j As Integer, a(1 To 4) As Integer
j = 0
Show
For i = 1010 To 9999 Step 2
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
If (a(1) <> a(2) And a(2) <> a(3) And a(3) <> a(4) And a(1) <> a(3) And a(1) <> a(4) And a(2) <> a(4)) Then
Print Tab((j Mod 5 + 1) * 7);
Print i;
j = j + 1
If (j Mod 5 = 0) Then Print
End If
Next
End Sub
17. 一位司机酒驾撞人逃跑。现场三人目击事件,但都没记住车号,只记下车号的一些特征。甲说:牌照的前两位数字是相同的;乙说:牌照的后两位数字是相同的;丙是位数学家,他说:四位的车号刚好是一个整数的平方。请根据以上线索求出车号。(车号为4位正整数,结果在TextBox中输出)
解:代码为:
Private Sub Form_Load()
Dim i As Integer, j As Integer, a(1 To 4) As Integer
j = 0
Show
For i = 1000 To 9999
a(1) = i Mod 10
a(2) = ((i - a(1)) / 10) Mod 10
a(3) = ((i - i Mod 100) / 100) Mod 10
a(4) = ((i - i Mod 1000) / 1000) Mod 10
If (Sqr(i) - Fix(Sqr(i)) = 0) Then
If (a(1) = a(2) And a(3) = a(4)) Then
Text1.Text = i
End If
End If
Next
End Sub
18. 用户利用InputBox函数随机输入12个0-100之间的整数,统计出小于60,60到70,70到80,80到90以及90到100范围的整数各有多少个?用Msgbox函数显示输出结果。
解:代码为:
Private Sub Command1_Click()
Dim a(1 To 12) As Integer, b(1 To 5) As Integer, i As Integer
For i = 1 To 12
a(i) = Val(InputBox("请输入第" & i & "个数"))
Next
For i = 1 To 12
Select Case a(i)
Case 0 To 59
b(1) = b(1) + 1
Case 60 To 69
b(2) = b(2) + 1
Case 70 To 79
b(3) = b(3) + 1
Case 80 To 90
b(4) = b(4) + 1
Case 90 To 100
b(5) = b(5) + 1
Case Else
End Select
Next
MsgBox ("小于60:" & b(1) & ",60到70:" & b(2) & ",70到80:" & b(3) & ",80到90:" & b(4) & ",90到100:" & b(5))
End Sub
19. 编制程序要求输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上的数字,否则输出两数字之和。结果利用Msgbox函数输出 。
解:代码为:
Private Sub Command1_Click()
Dim a As Integer, b As Integer, i As Integer
a = Val(InputBox("请输入a:"))
b = Val(InputBox("请输入b:"))
If (a * a + b * b > 100) Then
i = ((a * a + b * b) - (a * a + b * b) Mod 100) / 100
MsgBox ("平方和结果的百位为:" & i)
Else
MsgBox ("两数之和为:" & a + b)
End If
End Sub
20. 猜数游戏:由计算机随机产生一个2位正整数让人来猜,只能猜8次,如果人猜对了,则利用Msgbox函数显示计算机随机产生的该2位正整数并显示“You are so clever”,否则利用Msgbox函数给出提示,告诉人所猜的数是太大还是太小,直到人猜对为止,或者8次都猜不对给出提示“Game Over”。
解:代码为:
Private Sub Command1_Click()
Dim a As Integer, b As Integer, i As Integer
Randomize
a = Fix(Rnd(Time) * 90) + 10
For i = 1 To 8
b = Val(InputBox("请输入一个数"))
Select Case b
Case 0 To a - 1
MsgBox ("太小了!")
Case a + 1 To 100
MsgBox ("太大了!")
Case a
MsgBox ("You are so clever!")
Exit For
Case Else
MsgBox ("输入错误!")
End Select
If (i = 8) Then MsgBox ("GAME OVER!")
Next
End Sub