资源描述
循环结构及数组练习一、单选题1. 执行以下程序段后,i的值为(B)For i = 1 To 10 Step 2y = y + iNext iA) 10 B) 11 C) 12 D) 252. 假定有如下语句:Private Sub Command1_Click()Counter = 0For i = 1 To 4For j = 6 To 1 Step -2 Counter = Counter + 1Next jNext iLabel1.Caption = Str(Counter)End Sub程序运行后,结果为(B)A) 11 B) 12 C) 16 D) 203. 假定有如下语句:Private Sub Command1_Click()For i = 1 To 4If i = 1 Then x = iIf i = 4 Then x = x +1Print x;End Sub程序运行后,结果为(B)D) 3 4 5 6A) 1 2 3 4 B) 2 3 4 5C) 2 3 4 4 4.哪个程序段不能分别正确显示1!、2!、3!、4!的值A) For i = 1 To 4n = 1For j = 1 To i n = n * jNext jPrint nNext iB) For i = 1 To 4For j = 1 To i n = 1n = n * jNext jPrint nNext iC) n = 1For j = 1 To 4 n = n * j Print nNext jD) n = 1: j = 1Do While j = 4 n = n * j Print n j = j + 1 Loop5. 编写如下的事件过程:Private Sub Command1_Click()For i = 1 To 4For j = 0 To iPrint Chr$(65 + i);Next jPrintNext iEnd Sub程序运行后,在窗体上显示的内容是(A)A)BBCCCDDDDEEEEEB) ABBCCCDDDDC) B CC DDD EEEED) AABBB CCCC DDDDD6. 编写如下的事件过程:Private Sub Command1_Click()Dim x,n As Integerx = 1n = 0Do While x 10 Then Exit ForNextText1.Text = Str (n)End Sub运行后,文本框中显示的值是(B)A) 2B) 3C) 4D) 58. 编写如下的事件过程: Private Sub Command1_Click()c = 1234c1 = Trim (Str(c)For i = 1 To 4Print NextEnd Sub 运行程序后,窗体上显示如下11 21 2 31 2 3 4则应该在处填写的表达式是(B)Mid (c1,n,n)A) Right (c1,n)B) Left (c1,i)C) Mid (c1,n,1) D)9. 编写如下的事件过程: Private Sub Command1_Click()n = Val (Text1.text)For i = 2 To nFor j = 2 To Sqr(i)If i Mod j = 0 Then Exit ForNext jIf j Sqr(i) Then Print iNext iEnd Sub 该事件过程的功能是:( C)A) 输出n以内的奇数B) 输出n以内的偶数C) 输出n以内的素数D)输出n以内能被j整数的数10. 编写如下的事件过程:For i = 1 To 3For j = 5 to 1 Step -1 Print i*jNext j,i 则语句Print i*j的执行次数是(A)A) 15 B) 16 C) 1711以下程序段输出结果为(B)x = 1 y = 4Do Until y 4 x = x * y y = y + 1LoopPrint xA) 1B) 4C) 812执行下面的程序段后,x的值为(A)x = 5For i = 1 To 20 Step 2 x = x + i5Next iA) 21 B) 22 C) 2313.编写如下的事件过程:Private Sub Command1_Click() For i = 1 To 4x = 4 For j = 1 To 3x = 3For k = 1 To 2x = x + 6 Next kNext jNext iPrint xEnd Sub 程序运行后,输出结果是(B)A) 7 B) 15 C) 157D) 18D) 20D) 24D) 53814.有如下的程序段:For i = 1 To 3For j = 1 To i a = a + 1Next jNext i执行上面程序后,a的值为(C)A)3B)9C)6D)2115. 有如下的程序段:n = 0For i = 1 To 3For j = 1 To iFor k = j To 3 n = n + 1Next kNext jNext i执行上面程序段后,n的值为:(B)A) 3 B) 14 C) 9 D) 2116. 阅读程序Private Sub Form_Click()x = 50For i = 1 To 4y = InputBox (“请输入1 个整数”) y = Val (y)If y Mod 5 = 0 Then a = a + y x = yElse a = a + xEnd IfNext iPrint aEnd Sub程序运行后,单击窗体,在输入对话框中依次输入15、24、35、46,输出结果为(A) A) 100B) 50 C) 120D) 701.如有以下程序代码:Option Base 1Private Sub Command1_Click()d = 0c = 10x = Array(10, 12, 21, 32, 24)For i = 1 To 5If x(i) c Then d = d + x(i) c = x(i)Elsed = d - cEnd IfNext iPrint dEnd Sub程序运行后,在窗体上输出的内容为(C A)89B)99C)232下面语句定义的数组元素个数中(C)Dim arr(3 To 5, -2 To 2)A) 20B) 12 C) 153. 下面语句定义的数组元素个数中(D)Dim a(-3 To 4, 3 To 6)A) 18B) 28 C) 214. 如有以下程序代码:Private Sub Command1_Click() Dim arr1(10), arr2(10)For i = 1 To 10arr1(i) = 3 * iarr2(i) = arr1(i) * 3Next iText1.Text = Str(arr2(i / 2 - 0.1)End Sub程序运行后,在文本框显示的是(B)A) 36B) 45 C) 545. 如有以下程序代码:Option Base 1Private Sub Command1_Click()Dim aa = Array(1, 2, 3, 4)j = 1For i = 1 To 4s = s + a(i) * jj = j * 10Next iPrint sD) 77D) 24D) 32D) 63End Sub运行程序后,在窗体上输出的内容为(D)A)1234B)12C)34D)43216. 如有以下程序代码: Private Sub Command1_Click() Dim array1(10, 10) As IntegerDim i, j As IntegerFor i = 1 To 3For j = 2 To 4 array1(i, j) = i + jNext jNext iText1.Text = array1(2, 3) + array1(3, 4)End Sub运行程序后,在窗体上输出的内容为(A)A) 12B) 13C) 14D) 157. 假设通过复制、粘贴建立了一个命令按扭数组Commandl,则以下说法错误的是(D)A) 数组中每个命令按扭的名称(Name属性)均为CommandlB) 数组中每个命令按扭的大小都一样C) 数组中每个命令按扭可以使用同一个时间过程D) 用名称Commandl可以访问数组中每个命令按扭8. 如有以下程序代码: Private Sub Commandl_Click() Dim arrl(l0), arr2(l0) As Integer n = 3For i = l To 5arrl(i) = i arr2(n) = 2 * n + iNext iPrint arr2(n); arrl(n)End Sub运行程序后,在窗体上输出的内容为(A)A) ll 3 B) 3 llC) l3 3D) 3 l39. 如有以下程序代码:Private Sub Commandl_Click()Dim arr(3, 3) As IntegerFor i = l To 3For j = l To 3If j = i Or j = 3 - i + l Then arr(i, j) = lElsearr(i, j) = 0End IfNext jNext iPrint arr(1, 1) + arr(2, 2) + arr(3, 3)End Sub运行程序后,在窗体上输出的内容为(D)A)0B)1C)2D)310. 如有以下程序代码:Option Base 1Dim a() As IntegerPrivate Sub Form_Click()Dim i As Integer, j As IntegerReDim a(3, 2)For i = 1 To 3For j = 1 To 2a(i, j) = i * jNext jNext iReDim Preserve a(3, 4)For i = 3 To 4a(3, i) = i + 5Next iPrint a(3, 2); a(3, 4)End Sub运行程序后,在窗体上输出的内容为(B)A) 00B) 69 C) 712 D) 81311. 在窗体上话3个单选按扭,组成1个名为chkOption的控件数组。用于标识控件数组各元 素的参数是(B)A) TagB) IndexC) ListIndexD) Name12. 如有以下程序代码:Option Base 0Private Sub Command1_Click()Static city As Variantcity = Array(北京,上海,”天津,重庆)Print city(1)End Sub运行程序后,在窗体上输出的内容为(D)A)空白B)错误提示C)北京D)上海13. 如有以下程序代码:Option Base 1Private Sub Form_click()Dim i As IntegerDim a(8) As StringFor i = 1 To 8a(i) = LCase(Chr(Asc(A) + (i - 1)Next iFor Each b In aPrint UCase(b);Next bEnd Sub运行程序后,在窗体上输出的内容为(C)A)12 3 4 5 6 7 8B)abcdefghC) ABCDEFGH14. 如有以下程序代码:Option Base 1Private Sub Command1_Click(index As Integer) Dim arr1(12) As Integer, arr2(3) As Integer Dim sum As Integer sum = 2For i = 1 To 12 arr1(i) = iNext iFor i = 1 To 3arr2(i) = arr1(i * i)Next iFor i = 1 To 3sum = sum + arr2(i)Next iPrint sum End Sub运行程序后,在窗体上输出的内容为(A)A) 16 B) 24 C) 32 D) 3615. 如有以下程序代码:Option Base 1Private Sub Command1_Click()Dim arrDim start As Integer, finish As IntegerDim sum As Integer arr = Array(12, 4, 8, 16) start = LBound(arr) finish = UBound(arr) sum = 0For i = start To finishsum = sum + arr(i)Next i c = sum / finish Text1.Text = c End Sub运行程序后,在窗体上输出的内容为(B)A)40B)10C)12D)16二、简答题1.下面程序的功能是:计算 f=l-l/(2*3)+l/(3*4)-l/(4*5)+1/(19*20)Private Sub Form_Click()Dim f,i,sign As Integersign=1f = 1 for i=2 to 19f = f + sign / (i * (i + 1) sign=-1*signNext iPrint “f=”;fEnd Sub2下面的程序是在一个字符串变量中查找“at”,并用消息框给出查找结果的报告:没有找到 或找到的个数,程序如下:Public Sub Findat()Dim Str1 As StringDim length As IntegerDim sum As IntegerDim i As IntegerStr1 = InputBox (“请输入一个字符串”)length = 一len(Strl)i = 1sum = 0Do While i = arr(j) Then a = arr(j) arr(j) = arr(i) arr(i) = aEnd IfNext jNext iFor i = 1 To 10Print arr(i);Next iEnd Sub程序运行后,将把数组arr中的10个数按照升序排列。2. 如有以下程序代码,填写空格处Option Base 1Private Sub Command1_Click()Dim arr(10) As IntegerDim max As IntegerRandomizeFor i = 1 To 10 arr(i) = Int(Rnd * 1000)Next imax = arr(1)For i = 2 To 10If arr(i) max Thenmax = arr(i)End IfPrint arr(i)Next iPrint max:; maxEnd Sub程序运行后,产生10个01000之间的随机整数,放入数组arr中,然后输出其中最大值。3. 如有以下程序代码:Option Base 1Private Sub Command1_Click()Dim i As IntegerDim a(5) As IntegerDim b(5) As IntegerDim c(5) As Integern = 4For i = 1 To na(i) = i m = n + i - 1 b(i) = mNext ic(5) = 0For k = 1 To nc(k) = a(k) * b(k)c(5) = c(5) + c(k)Next kPrint a(3); b(4); c(5)End Sub运行程序后,在窗体上输出的内容为3 7604. 如有以下程序代码,填写空格处:Option Base 1Private Sub Command1_Click()Dim arr1(3, 3) As IntegerDim arr2(3) As IntegerRandomizeFor i = 1 To 3For j = 1 To 3arrl(i, j) = Int(Rnd * 100)Next jNext iFor i = l To 3For j = l To 3arr2(i) = arr2(i) + arr1(i, j)Next jNext iFor i = l To 3For j = l To 3Print arrl(i, j);Next jPrintNext ism = arr2(1)ns = 1For i = 2 To 3If arr2(i) sm Then sm = arr2(i) ns = iEnd IfNext iPrintPrint 数值最大的一行是:第; ns; 行,; 该行的和是:; smEnd Sub以上程序功能:用随机数生成一个3X3的矩阵,然后求出该矩阵3行中元素之和最大的那 一行,并输出该行的行号及该行的和。-! x|SI S4 T35Q S 31706995数值最大的一行是:单1行诊行的和是:23S C umn-i :tl dl三、编程题1.编写程序,把下面的数据输入1 个二维数组中:25367813122688937518223256443658然后执行以下操作(1) 输出矩阵2 个对角线上的数(2) 分别输出各行和各列的和(3) 交换第 1 行和第 3 行的位置(4) 交换第 2 列和第 4 列的位置(5) 输出处理后的数组程序代码:Option Base 1Private Sub Form_click()Dim mat(4, 4) As IntegerDim n, m As Integern = 4m = 4输入矩阵For i = 1 To nFor j = 1 To mmat(i, j) = InputBox(请输入数组第& i & 行第& j & 列元素值) Val (mat(i, j)Next jNext i输入初始化矩阵PrintPrint 初始矩阵为:PrintFor i = 1 To nFor j = 1 To mPrint Tab(6 * j); mat(i, j);Next jPrintNext i输出矩阵对角线上的数PrintPrint 矩阵对角线上的数为:PrintFor i = 1 To nFor j = 1 To m If i = j ThenPrint Tab(6 * j); mat(i, j);End IfNext jNext iPrintFor i = 1 To nFor j = 1 To mIf i + j = 5 ThenPrint Tab(6 * i); mat(i, j); End IfNext jNext i交换第一行和第三行PrintFor j = 1 To mt = mat(1, j)mat(1, j) = mat(3, j)mat(3, j) = tNext jPrintPrint 交换第一行和第三行后的矩阵为:PrintFor i = 1 To nFor j = 1 To mPrint Tab(6 * j); mat(i, j);Next jPrintNext i交换第二列和第四列For i = 1 To nt = mat(i, 2)mat(i, 2) = mat(i, 4)mat(i, 4) = tNext iPrintPrint 交换第二列和第四列后的矩阵为:PrintFor i = 1 To nFor j = 1 To mPrint Tab(6 * j); mat(i, j);Next jPrintNext iEnd Sub 2编写程序,实现矩阵转置,将1个nXm的矩阵的行和列互换。其中n和m元素的值为随 机产生。例如:A 矩阵为A =123 “ 456 “转置后的 B 矩阵为1B =23程序代码:Option Base 1Private Sub Form_click()Const n = 3Const m = 4Dim a(n, m), b(m, n) As IntegerFor i = 1 To nFor j = 1 To ma(i, j) = Int(Rnd * 90) + 10 Next jNext iFor i = 1 To nFor j = 1 To m b(j, i) = a(i, j)Next jNext iPrintPrint 矩阵 a 转置前:PrintFor i = 1 To nFor j = 1 To mPrint Tab(5 * j); a(i, j); Next j PrintNext iPrintPrint 矩阵 b 转置后:PrintFor i = 1 To mFor j = 1 To nPrint Tab(5 * j); b(i, j);Next jPrintNext iEnd Sub
展开阅读全文