1、三、循环结构编程1. 求自然数 1100 之间奇数之和。交 1代码 1Private Sub Command1_Click()Dim i As Integers = 0For i = 1 To 100 Step 2s = s + iNextPrint “1 到 100 的奇数和为:“ sEnd Sub代码 2(双循环实现)Private Sub Command1_Click()Dim t As Long, s As Longs = 0for a=1 to 20t = 1For i = 1 To at = t * iNexts = s + tnextPrint “s=“; sEnd Sub4.编
2、写程序,将 26 个字母逆序输出。代码 1:Dim s As StringS = “ABCDEFGHIJKLMNOPQRSTUVWXYZ“Print sFor i = 26 To 1 step -1 或:For i = len(s) To 1 step -1Print Mid(s,i,1)Next iEnd Sub代码 2:Private Sub Command1_Click()Dim s As String, p As Strings = “ABCDEFGHIJKLMNOPQRSTUVWXYZ“ 或:s= text1.textPrint sFor i =len(s) To 1 Step -1
3、p = p For i = 0 To 9a(i) = Int(Rnd * 10)Print a(i);Nextt = a(9) 提前将最后一个元素放在一个变量中For i = 9 To 1 Step -1a(i) = a(i - 1)Nexta(0) = t 将最后一个元素覆盖第一个元素PrintPrint “移位后“;For i = 0 To 9Print a(i);NextEnd Sub9. 编程将一维数组 A(10)中数组元素循环向后移动一个位置,最后三个元素移动到第一个、第二个、第三个元素的位置。代码:Private Sub Command1_Click()Dim a(9) As In
4、teger, t As IntegerClsPrint “移位前 “;For i = 0 To 9a(i) = Int(Rnd * 10)Print a(i);NextFor j = 1 To 3 控制位移次数t = a(9)For i = 9 To 1 Step -1a(i) = a(i - 1)Nexta(0) = tPrintPrint “移位后“;For i = 0 To 9Print a(i);NextPrintNextEnd Sub10. 编写一个程序,用户连续输入 N 个值,直到输入非数值数据为止。最后输出其中最大的一个数。代码:(第 3 章作业 p71-72)交 2Privat
5、e Sub Command1_Click()ClsDon = InputBox(“输入数值:“)If Not IsNumeric(n) ThenExit DoElseLabel2.Caption = Label2.Caption n;If Val(n) Val(Max) Then 不加函数 Val 按字符串比较Max = nEnd IfEnd IfLoopPrintPrint MaxLabel3.Caption = MaxEnd Sub11. 编写程序,在窗体上输出由 1、2、3、4、5 这五个数字组成的所有可能的两位数,并统计它们的个数。代码 1:Private Sub Command1_C
6、lick()Dim i%, j%, n%, m%For i = 1 To 5For j = 1 To 5n = i * 10 + j 20+1,20+2.20+5 30+1,30+.3+5-50+1,50+2.50+5 ,通式:i*10+jPrint n;m = m + 1If m Mod 5 = 0 ThenPrintEnd IfNext jNext iEnd Sub代码 1:Private Sub Command1_Click()Dim i%, p$, n%Print “由 1、2、3、4、5 这五个数字组成的所有可能的两位数如下 :“PrintFor i = 1 To 5For j =
7、1 To 5p = p p nEnd Sub12. 在窗体的单击事件中编写程序,从键盘上任意输入 3 个数,输出中间数。代码:Private Sub Command1_Click()Dim a(1 To 3) As Integer, t As IntegerPrint “输入的数据是:“;For i = 1 To 3a(i) = InputBox(“输入数据“)Print a(i);NextPrintIf a(1) a(3) Thenm = a(2)ElseIf a(1) a(3) Thenm = a(3)Elsem = a(1)End IfPrint “中间数是:“; mEnd Sub13.
8、 编写程序,求 s 的值,s=123+2 34+n(n+1)(n+2)的值。n 由程序输入。代码:交 1Private Sub Command1_Click()Dim l%, s%, a%l = Val(Text1.Text)For n = 1 To la = n * (n + 1) * (n + 2)s = s + aNextPrint sEnd Sub14.求 S=a+aa+aaa+.+aaa.a 的值,其中 a 和 n 的值由用户自己输入例如,当 a=3,n=4 时,s3 十 33 十 333 十 3333。交代码(利用字符串重复的算法):Private Sub Form_Click()
9、Dim s%, i%, n%, a$s = 0a = Text1.Textn = Text2.TextFor i = 1 To ns = s + Val(String(i, a)NextPrint sEnd Sub代码 2:该程序求 S=a+aa+aaa+.+aaaa.aaa 的值,其中 a 和 n 的值由用户自己输入。 例如,当 a=3,n=4 时,s 3 十 33 十 333 十 3333。补充程序。(利用 S=3*100+(3+3*101)+(33+3*102)+(333+3*103)算法):Private Sub Command1_Click()Dim a As Integer, n
10、As Integer, s As Double, t As Doublen = InputBox(“请输入 n“) a = InputBox(“请输入 a“) For i = 1 To 【1】n【2】 = t + a * 10 (i - 1) t s = 【3】s+tNext iPrint a, n,sEnd Sub15. 设纸的厚度为 0.5 毫米,将纸对折,再对折,求至少对折多少次,纸张的厚度能达到珠穆朗玛峰的高度 8848 米。交 1代码:Private Sub Command1_Click()n = 0.5s = 0Do While n 8848.13 * 1000 ThenExit
11、DoElses = s + 1End IfLoopLabel1.Caption = s NextPrintNextEnd Sub16. 编程计算从 200 到 1000 之间的所有 4 的倍数之和。交 1代码:Private Sub Command1_Click()Dim I As Integer, s As Longs = 0For I = 200 To 1000If I Mod 4 = 0 Then 或 If Int(I / 4) = I / 4 Thens = s + IEnd IfNextPrint “200 到 1000 之间的所有 4 的倍数之和“; sEnd Sub 17. 编写
12、程序,通过 InputBox 函数从键盘上输入 4 个数, 计算并在窗体上显示这 4 个数的和及平均值。代码:Private Sub Command1_Click()Dim I As Integer, s As LongDim a(1 To 4) As IntegerFor I = 1 To 4a(I) = InputBox(“输入第“ s = s + a(I)NextPrintPrint “s=“; sEnd Sub12. 编写程序,输入一个数,判定该数是否为素数,如果是素数,则输出“该数是素数”的信息,否则输出“该数不是素数”的信息。交 1代码 1(利用循环变量产超出终值的方法):Priv
13、ate Sub Command1_Click()Dim x As Integer, i As Integerx = InputBox(“请输入一个数“)For i = 2 To x - 1If x Mod i = 0 ThenPrint “bsss“Exit ForEnd IfNextIf i = x ThenPrint “shi“End IfEnd Sub代码 2(设素数标志的方法):Private Sub Command1_Click()Dim x As Integer, i As Integer, a%a = 1 素数标志x = InputBox(“请输入一个数“)For i = 2 T
14、o x - 1If x Mod i = 0 Thena = 0Print “bsss“Exit ForEnd IfNextIf a = 1 ThenPrint “shi“End IfEnd Sub12. 编写程序,输出 50-100 之间的所有素数 . 交 2代码 1(利用素数标志方法)Private Sub Command1_Click()Dim i As Integer, j As Integer, a%, p$For i = 50 To 100a = 1For j = 2 To i - 1If i Mod j = 0 Thena = 0Exit ForEnd IfNextIf a = 1
15、 ThenPrint i;p = p p = p & i & “ “End IfNextPrint pEnd Sub13. 编程序计算表达式 1/2+2/3+3/4+4/5 的值。交 1代码 1:Private Sub Command1_Click()Dim sum As Double, x As Doublesum = 0n = 0For i = 1 To 5 x = n / in = n=n+1sum = sum +xNextPrint sumEnd Sub代码 2(利用分子和分母间的变化规律):Dim sum As Double, x As Doublesum = 0n = 0For i = 1 To 5 x = n / in = n=n+1sum = sum +xNextPrint sumEnd Sub