Private Sub Form_click()
Dim a(1 To 6) As Integer, i As Integer, key As Integer, searchn As Integer
For i = 1 To 6
a(i) = Val(InputBox("输入第" + Str(i) + "个数"))
Next i
searchn = -1
key = InputBox("输入要查找的数据")
For i = LBound(a) To UBound(a)
If key = a(i) Then
searchn = i
Exit For
End If
Next i
If searchn = -1 Then
Print "数据未找到"
Else
Print "找到的数据为:"; a(searchn)
End If
End Sub
请问这个vb语句中的searchn=-1是什么含义
答案:2 悬赏:70 手机版
解决时间 2021-03-05 23:21
- 提问者网友:人傍凄凉立暮秋
- 2021-03-05 06:19
最佳答案
- 五星知识达人网友:动情书生
- 2020-09-20 00:07
赋初值,当以下标跟踪方式搜索到数组a()中所查找的数据时,便被赋值为当前下标值。
说白了,赋值-1只是为了验证有没有搜索到数据,只要不与a()数组的下标冲突赋值什么数都无所谓。这个程序完全可以将该语句省略,因为0和1 to 6不冲突。
Private Sub Form_click()
Dim a(1 To 6) As Integer, i As Integer, key As Integer, searchn As Integer
For i = 1 To 6
a(i) = Val(InputBox("输入第" + Str(i) + "个数"))
Next i
key = InputBox("输入要查找的数据")
For i = LBound(a) To UBound(a)
If key = a(i) Then
searchn = i
Exit For
End If
Next i
If searchn Then
Print "数据未找到"
Else
Print "找到的数据为:"; a(searchn)
End If
End Sub
说白了,赋值-1只是为了验证有没有搜索到数据,只要不与a()数组的下标冲突赋值什么数都无所谓。这个程序完全可以将该语句省略,因为0和1 to 6不冲突。
Private Sub Form_click()
Dim a(1 To 6) As Integer, i As Integer, key As Integer, searchn As Integer
For i = 1 To 6
a(i) = Val(InputBox("输入第" + Str(i) + "个数"))
Next i
key = InputBox("输入要查找的数据")
For i = LBound(a) To UBound(a)
If key = a(i) Then
searchn = i
Exit For
End If
Next i
If searchn Then
Print "数据未找到"
Else
Print "找到的数据为:"; a(searchn)
End If
End Sub
全部回答
- 1楼网友:往事埋风中
- 2020-08-21 02:18
额
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯