1、 问题 3 2.25 分 保存 从键盘输入一段英文文章(都是小写字母),统计一下该段文章中26个英文字母的使用情况。 Private Sub Form_Click() Dim c(1 To 26) As Integer Dim n As Integer, k As Integer Dim s As String, xs As String For i = 1 To 26 c(i) = 0 Next i (1) (2) F
2、or i = 1 To n (3) If xs >= "a" And xs <= "z" Then (4) (5) End If Next i For i = 1 To 26 Print Chr(96 + i); "----"; c(i) Next i End Sub (1) (2) (3) (4)
3、 (5) 1. n = Len(s) 2. c(k) = c(k) + 1 3. k = Asc(xs) – 96 4. s = InputBox("s=") 5. xs = Mid(s, i, 1) 问题 4 2.25 分 保存 单击窗体,产生20个从1到600之间的随机整数,并以每行5个把数据显示在窗体上,并在窗体上显示20个数据中的所有偶数之和。 Private Sub Form_Click() (1) s = 0 For
4、 i = 1 To 20 Randomize (2) Print x; (3) If (4) Then s = s + x Next i (5) End Sub (1) (2) (3) (4) (5) 1. If i mod 5 =0 Then Print 2. Dim i
5、 As Integer, x As Integer, s as Long 3. Print "s="; s 4. x Mod 2 = 0 5. x = Int(600*Rnd+1) 问题 5 2.25 分 保存 实现文本框的复制、剪切、粘贴、删除功能 Private Sub Command1_Click() (1) Clipboard.SetText Text1.SelText End Sub Private Sub Command2_Click() Clipbo
6、ard.Clear (2) (3) End Sub Private Sub Command3_Click() (4) End Sub Private Sub Command4_Click() (5) End Sub (1) (2) (3) (4) (5) 1. Text1.SelText = "" 2. Text1.SelText = ""
7、 3. Text1.SelText = Clipboard.GetText 4. Clipboard.Clear 5. Clipboard.SetText Text1.SelText 问题 6 2.25 分 保存 调用VB的RND和INT函数,在当前工作表的A列的1—10行输入序号1—10,B列的1—10行填入0—1之间的随机数,C列的1—10行填入两位正整数,最后在C列的11行求出该列的最大数(调用EXCEL的内部MAX函数)。 Sub random() Dim i As Integer, c As S
8、tring For i = 1 To 10 (1) (2) Next i For i = 1 To 10 (3) Range(c) = Rnd Next i For i = 1 To 10 c = "C" & i (4) Next i (5) E
9、nd Sub (1) (2) (3) (4) (5) 1. c = "B" & i 2. Range(c) = i 3. Range("C11") = Application.WorksheetFunction.Max(Range("C1:C10")) 4. Range(c) = Int(Rnd * 90) + 10 5. c = "A" & i 问题 7 2.25 分 保存 编制Form_Click过程,用近似公式求自然对数的底数e
10、的值,直到末项小于10-4为止,结果显示在窗体上。 Private Sub Form_Click() Dim s As Single, x As Single, a As Single a = 1 (1) i = 1 Do (2) (3) (4) i = i + 1 Loop (5)
11、 End Sub (1) (2) (3) (4) (5) 1. While a >= 0.0001 2. Print "e="; s 3. s = s + a 4. a = a / i 5. s = 1 问题 8 2.25 分 保存 从键盘输入一个字符串,然后将该字符串中的每个字符按ASCII码值的大小从小到大进行重新组合输出。如:输入abc123XYZ,处理后应输出123XYZabc。(难度系数**) Private Su
12、b Form_Click() Dim c(100) As String Dim n As Integer Dim s As String, nc As String, temp As String s = InputBox("s=") (1) For i = 1 To n (2) Next i For i = 1 To n - 1 For j = i + 1 To n (3)
13、 (4) c(i) = c(j) c(j) = temp End If Next j Next i nc = "" For i = 1 To n (5) Next i Print nc End Sub (1) (2) (3) (4) (5) 1. nc = nc & c(i) 2. temp = c(i) 3. If c(i
14、) > c(j) Then 4. n = Len(s) 5. c(i) = Mid(s, i, 1) 问题 9 2.25 分 保存 利用二维数组编程输出“杨辉三角形”图案,图案行数n从键盘输入。 Private Sub Form_Click() Dim x() As Integer Dim n As Integer (1) (2) (3) For i=2 To n
15、 For j=1 To i (4) Next j Next i For i=1 To n For j=1 To i Print x(i,j); Next j (5) Next i End Sub (1) (2) (3) (4) (5) 1. Print 2. Redim x(n,n) 3. n = InputBox("n=") 4.
16、 x(i,j) = x(i-1,j) + x(i-1,j-1) 5. x(1,1)=1 问题 10 2.25 分 保存 随机产生100个学生的成绩(35~98之间),统计各分数段的人数。即0~9、10~19、、20~29、30~39、40~49、50~59、60~69、70~79、80~89、90~99,并输出结果。 Private Sub Form_Click() Dim a(1 To 100) As Integer (1) For i = 1 To 100
17、 (2) Next i For i = 0 To 9 c(i) = 0 Next i (3) (4) c(k) = c(k) + 1 Next i (5) Print 10*i;"~";10*i+9;"----";c(i) Next i End Sub (1) (2) (3) (4)
18、 (5) 1. For i = 1 To 100 2. For i = 0 To 9 3. Dim c(9) As Integer 4. k = Int(a(i) / 10) 5. a(i) = Int(Rnd * 64)+35 问题 11 2.25 分 保存 建立一个“猜数程序”。单击窗体,产生一个2位的随机正整数。在文本框Text1中输入你猜测的数字,单击按钮Command1在窗体上显示猜测结果:如果你输入的数字比程序生成的随机数要大,则显示“大了”;如果比程序生成的随机数要小则“小了”;
19、然后继续输入数字进行猜测。如果猜中,则显示“恭喜你猜中啦!”,并用消息框显示一共猜了几次。 Dim n As Integer, k As Integer (1) Randomize (2) End Sub Private Sub Command1_Click() Dim x As Integer (3) k = k + 1 If x > n Then Print x; "大了" (4)
20、 Print x; "小了" Else Print x; "恭喜你猜中了!" MsgBox "一共猜了" & k & "次" End If (5) (1) (2) (3) (4) (5) 1. n = Int(Rnd * 90) + 10 2. Private Sub Form_Click() 3. ElseIf x < n Then 4. End Sub 5. x = Text1.Text
21、 问题 12 2.25 分 保存 单击窗体,从输入框InputBox输入x和n,并计算下列表达式的值,直至末项小于 为止,结果显示在窗体。 Private Sub Form_Click() Dim s As Single, x As Single, a As Single x = Val(InputBox("输入x")) (1) s = 1 i = 0 Do (2) (3
22、) s = s + a Loop (4) (5) End Sub (1) (2) (3) (4) (5) 1. i = i + 1 2. a = a * x / i 3. Print "s="; s 4. While a >= 0.00001 5. a = 1 问题 13 2.25 分 保存 从1到1
23、0000中找出这样的数,该数各个位的数字的阶乘相加之和等于该数,并将结果输出。已知fact函数用于计算阶乘。 Private Function fact(ByVal k As Integer) As Long Dim i As Integer (1) For i = 2 To k fact = fact * i Next i End Function Private Sub Form_Click() Dim k as integer, a as string, n as long, i as integer Dim p As
24、 Integer For k = 1 To 10000 (2) n = 0 For i= 1 To Len(a) (3) (4) Next i (5) Next k End Sub (1) (2) (3) (4) (5) 1. fact = 1 2. If n = k Then
25、Print k 3. n = n + fact(p) 4. p = Val(Mid(a, i, 1)) 5. a = CStr(k) 问题 14 2.25 分 保存 输入10个数放入数组中,将第1个数移到最后位置,其余数依次往前移动一个位置。 Private Sub Form_Click() (1) Dim t As Integer For i = 1 To 10 (2) Next i For
26、i = 1 To 10 Print x(i); Next i Print (3) For i = 1 To 10-1 (4) Next i x(10) = t For i = 1 To 10 (5) Next i Print End Sub (1) (2) (3) (4) (5) 1. Dim x(10) As
27、Integer 2. x(i) = InputBox("x(" & i & ")=") 3. Print x(i) 4. t = x(1) 5. x(i) = x(i+1) 问题 15 2.25 分 保存 随机产生200个互不相等的三位正整数并按每10个一行输出。 Private Sub Form_Click() Dim a(200) As Integer Dim k As Integer,x As Integer Dim f As Integer (1)
28、 Do While k<200 x = Int(Rnd*900)+100 f=1 For i=1 To k If a(i)=x Then (2) Next i If f=1 Then (3) (4) End if Loop For i=1 To k Print a(i); (5)
29、 Next i End Sub (1) (2) (3) (4) (5) 1. k=k+1 2. f=0 3. a(k)=x 4. If i Mod 10=0 Then Print 5. k=0 问题 16 2.25 分 保存 随机产生10个6—100之间的偶数,对每个数进行哥德巴赫猜想(Goldbach Conjecture)验证。其中判断一个整数是否为素数用以下Prime函数。 Function Prime(x As In
30、teger) As Boolean Dim i As Integer Prime = True For i = 2 To x - 1 If x Mod i = 0 Then Prime = False Exit For End If Next i End Function Sub Goldbach() Dim i As Integer, a As Integer, b As Integer ActiveSheet.Cells.Clear
31、 Cells(1, 1) = ">6的偶数" Cells(1, 2) = "第一个素数" (1) For i = 1 To 10 (2) Next i For i = 2 To 11 (3) If Prime(a) And Prime(Cells(i, 1) - a) Then (4)
32、 (5) Exit For End If Next a Next i End Sub (1) (2) (3) (4) (5) 1. Cells(1, 3) = "第二个素数" 2. Cells(i, 2) = a 3. For a = 3 To Cells(i, 1) / 2 4. Cells(i, 3) = Cells(i, 1) - a 5.
33、Cells(i + 1, 1) = (Int(Rnd * 48) + 3) * 2 问题 17 2.25 分 保存 单击窗体,根据输入框所输入的百分制成绩score,在窗体上显示相应的等级,即“优秀”(score>=90)、“良好”(80<=score<90)、“中”(70<=score<80)、“及格”(60<=score<70)、“不及格”(score<60)。 Private Sub Form_click() (1) score = Val(InputBox("请输入成绩"
34、)) If (2) Then Print "优秀" ElseIf score >= 80 Then Print "良好" ElseIf score >= 70 Then Print "中" (3) (4) Else Print "不及格" (5) End Sub (1) (2) (3) (
35、4) (5) 1. Print "及格" 2. End If 3. ElseIf score >= 60 Then 4. Dim score As Integer 5. score >= 90 问题 18 2.25 分 保存 单击窗体,从输入框InputBox输入一个整数,判断其是否为素数。例如输入7,则屏幕上显示“7是素数”;如果输入的是9则屏幕上显示“9不是素数”。 Private Sub Form_Click() Dim i As Integer, n As Intege
36、r, Prime As String n = Val(InputBox("n=")) If n < 2 Then (1) Else (2) If n Mod i = 0 Then Exit For Next i (3) (4) Else Pri
37、me = "不是素数" End If End If (5) End Sub (1) (2) (3) (4) (5) 1. Prime = "不是素数" 2. For i = 2 To n-1 3. If i > n-1 Then 4. Print n & Prime 5. Prime = "是素数" 问题 19 2.25 分 保存 1、 在文本框Text1中输入
38、一个正整数,单击按钮Command1,判断该数的奇偶性,结果显示在标签框label2中,要求偶数用蓝色显示,奇数用红色显示。 Private Sub Command1_Click() Dim x As Integer (1) If (2) Then Label2.ForeColor = vbBlue (3) (4) (5) Label2.Cap
39、tion = x & "是奇数" End If End Sub (1) (2) (3) (4) (5) 1. x Mod 2 = 0 2. Else 3. Label2.ForeColor = vbRed 4. x = Val(Text1.Text) 5. Label2.Caption = x & "是偶数" 问题 20 2.25 分 保存 编写程序,在工作表上打印九九乘法表如下。 Sub print99() D
40、im i As Integer, j As Integer (1) For i = 1 To 9 Cells(1, i + 1) = i Cells(i + 1, 1) = i Next i (2) (3) (4) Next j Next i Range("A:A").Font.Bold
41、 True Range("A:A").HorizontalAlignment = xlCenter (5) Range("1:1").HorizontalAlignment = xlCenter End Sub (1) (2) (3) (4) (5) 1. Range("1:1").Font.Bold = True 2. Cells(i + 1, j + 1) = i * j 3. For i = 1 To 9 4. Activ
42、eSheet.Cells.Clear 5. For j = i To 9 问题 21 2.25 分 保存 在工作表1上创建“控件工具箱”命令按钮“产生数据”(CommandButton1)和“判断”(CommandButton2)。 单击“产生数据”按钮完成在A1:A10上产生两位随机正整数;单击“判断”按钮完成将其中重复数用红色标注。 Private Sub CommandButton1_Click() Dim i As Integer (1) For i
43、 = 1 To 10 (2) Next i End Sub Private Sub CommandButton2_Click() Dim i As Integer, j As Integer For i = 1 To 9 (3) (4) (5) End If Next j
44、 Next i End Sub (1) (2) (3) (4) (5) 1. For j = i + 1 To 10 2. If Cells(i, 1) = Cells(j, 1) Then 3. ActiveSheet.Cells.Clear 4. Cells(i, 1) = Int(Rnd * 90) + 10 5. Cells(j, 1).Font.Color = vbRed 问题 22 2.25 分 保存 从文本框
45、Text1输入一个二进制正整数,单击窗体,在窗体上打印出相应的十进制数;且文本框中只允许输入0和1。 Private Sub Form_Click() Dim s As String, a As String, n As Integer, x As Long (1) n = Len(s) For i = 1 To n (2) x = x + Val(a) * 2 ^ (i - 1) Next i (3) End Sub
46、Private Sub Text1_KeyPress(KeyAscii As Integer) If (4) Then (5) End If End Sub (1) (2) (3) (4) (5) 1. KeyAscii < Asc("0") Or KeyAscii > Asc("1") 2. Print x 3. KeyAscii = 0 4. s = Text1.Text 5. a = Mid
47、s, n + 1 - i, 1) 问题 23 2.25 分 保存 随机产生两位数的整数放入一个5行5列的二维数组中,求该二维数组对角线上最大值所在的行和列。 Private Sub Form_Click() Dim a(1 To 5, 1 To 5) As Integer, max As Integer Dim pi As Integer, pj As Integer Randomize For i = 1 To 5 For j = 1 To 5 a(i, j) = Int(R
48、nd * 90 + 10) Next j Next i For i = 1 To 5 For j = 1 To 5 Print a(i, j); Next j (1) Next i max = a(1, 1) (2) pj = 1 For i = 1 To 5 For j = 1 To 5 (3) (4)
49、 max = a(i, j) pi = i (5) End If End If Next j Next i Print "对角线上最大值在第"; pi; "行和第"; pj; "列" End Sub (1) (2) (3) (4) (5) 1. pi = 1 2. If i = j Or i + j = 6 Then 3
50、 pj = j 4. Print 5. If max < a(i, j) Then 问题 24 2.25 分 保存 产生10个[30,50]的随机整数,并按从小到大的顺序打印出来,要求调用Swap子过程。 Public Sub Swap(a As Integer, b As Integer) Dim temp As Integer temp = a (1) (2) End Sub Private Sub Form_Clic
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818