-2

任何人都可以知道如何从 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
4

2 回答 2

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
于 2012-06-25T10:56:25.737 回答
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
于 2012-06-25T10:57:53.603 回答