中国大学MOOC: 数组a是个整数数组且已有数据,如要在a中查找InputBox中输入的数字,并输出其下标。程序如下,请在_________处填入适当的内容,完善程序。 Dim i%, idx%, key% key = Val(InputBox("输入要查找的数字")) idx = -1 For i = 0 To UBound(a) If a(i) = key Then _________ Exit For End If Next If idx >= 0 Then MsgBox ("下标为" & idx) Else MsgBox ("找不到此数字") End If
举一反三
- 下列Find函数的功能是在数组里查找数字key,返回该key在数组中的下标,如果找不到则返回-1。请在_________处填入适当的内容完善程序。 Function Find(ByRef a%(), ByVal key%) As Integer Dim idx%, i% idx = -1 For i = LBound(a) To UBound(a) If a(i) = key Then _________ Exit Function End If Next Find = idx End Function
- 在窗体上有一命令按钮Command1,其Click事件代码如下: Private Sub Command1_Click() Dim a%, b%, i%, n% For i = 1 To 10 n = InputBox("请输入一个整数", "输入数据框", 2) If n Mod 2 = 0 Then a = a + 1 Else b = b + 1 End If Next i MsgBox ("运行结果a=" & str(a) & ", b=" & str(b)) End Sub
- 若有数组Dim a%(10)且每个元素有值,统计前一个元素大于后一个元素的次数,结果存放在变量n中。则下列语句正确的是___________。 A: Dim i%, n% For i = 1 To 10 If a(i) > a(i + 1) Then n = n + 1 End If Next MsgBox(n) B: Dim i%, n% For i = 1 To 10 If a(i) > a(i + 1) Then n = a(i) End If Next MsgBox(n) C: Dim i%, n% For i = 0 To 9 If a(i) > a(i + 1) Then a(i) = n + 1 End If Next MsgBox(n) D: Dim i%, n% For i = 0 To 9 If a(i) > a(i + 1) Then n = n +1 End If Next MsgBox(n)
- 如下程序的功能是随机产生10个大写字母保存到数组。请在_________处填入适当的内容(不要空格),完善程序。 Private Sub Command1_Click() Dim a(9) As String, i% For i = 0 To 9 a(i) = Chr(Rnd * 26 + _________) Next End Sub
- 要将字符串数组a中所有学生姓名按数组下标顺序追加到列表框List1中,可以使用的程序段为( )。 A: List1.AddItem a B: For i = 0 To UBound(a) - 1 List1.AddItem a(i), 0 Next i C: For i = 0 To UBound(a) – 1List1.List(i)=a(i)Next i D: For i = 0 To UBound(a) - 1 List1.AddItem a(i) Next i