如何从整数数组中查找整数?

时间:2012-06-25 08:23:08

标签: arrays vb6

任何人都知道如何从vb6中的整数数组中找到一个整数吗?

 Dim myArray(2) As Integer
myArray(1) = 1001
myArray(2) = 1002

Dim searchTerm As Integer
searchTerm = 1005

Dim flag As Boolean
flag = True

Dim temp As Variant

For Each temp In myArray
If temp = searchTerm Then
flag = False
End If
Next temp

If flag = False Then
MsgBox "find"
End If

我通过使用For Each语句得到了解决方案,但我希望解决方案使用Do..Loop ??

修改

Dim myArray(2) As Integer 
myArray(0) = 1000 
myArray(1) = 1001 
myArray(2) = 1002 

'Initialise Search Term 
Dim searchTerm As Integer 
searchTerm = 1001 

'Check if a value exists in the Array 
If UBound(Filter(myArray, searchTerm)) >= 0 And searchTerm <> "" Then 
  MsgBox ("Search Term SUCCESSFULLY located in the Array") 
Else 
  MsgBox ("Search Term could NOT be located in the Array") 
End If

2 个答案:

答案 0 :(得分:2)

你可以简单地说:

Dim i As Integer, found As Boolean
Do While i <= UBound(myArray) And Not found
    If (myArray(i) = searchTerm) Then
        found = True
    Else
        i = i+1
    End If
Loop

If (found) Then Msgbox "found @ " & i

答案 1 :(得分:0)

以下代码工作文件

Dim myArray(2) As Integer
myArray(0) = 1000
myArray(1) = 1001
myArray(2) = 1002

Dim searchTerm As Integer
searchTerm = 1005

Dim flag As Boolean
flag = True

Dim i As Integer
Dim lb As Integer
Dim hb As Integer
lb = LBound(myArray)
hb = UBound(myArray)

Do While (lb < hb)
  Dim j As Integer
  If searchTerm = myArray(j) Then
    flag = False
  End If
  j = j + 1
  lb = lb + 1
Loop

If flag = False Then
  MsgBox "find"
Else
  MsgBox "not find"
End If