我試圖找到陣列不同點的值。當我運行代碼時,它總是轉到The value doesn't exists,我也不知道如何計算相同的值r。
r = 0
c = txtbbus.Text
For i = 0 To n - 1
For j = 0 To n - 1
If a(i, j) = c Then
txtbres.Text = "The value exists " & r & " and it's in the position (" & i & ", " & j & ") "
Else
txtbres.Text = "The value doesn't exists"
End If
Next j
Next i
這就是我初始化的方式a:
txtbmatriz.Text = ""
For i = 0 To n - 1
For j = 0 To n - 1
a(i, j) = CInt((100 * Rnd()) 1)
txtbmatriz.Text = a(i, j) & " "
m = a(i, j)
l = 1
Next j
txtbmatriz.Text = vbCrLf
Next i
uj5u.com熱心網友回復:
幾乎可以肯定的是,當您找到匹配項時,您不會跳出回圈。您的代碼只會向您顯示陣列中最后一個元素的結果,因為您始終會一直搜索到最后一個元素。一旦找到匹配項,就沒有必要再看下去了,事實上,這樣做是有害的。一旦找到匹配項,請停止查找。
查找單個/第一個匹配項:
Dim rng As New Random
Dim matrix = New Integer(9, 9) {}
For i = 0 To matrix.GetUpperBound(0)
For j = 0 To matrix.GetUpperBound(1)
matrix(i, j) = rng.Next(1, 101)
Next
Next
Dim target = rng.Next(1, 101)
Dim message As String
For i = 0 To matrix.GetUpperBound(0)
For j = 0 To matrix.GetUpperBound(1)
If matrix(i, j) = target Then
message = $"{target} found at ({i},{j})"
Exit For
End If
Next
If message IsNot Nothing Then
Exit For
End If
Next
Console.WriteLine(If(message, $"{target} not found"))
查找所有匹配項:
Dim rng As New Random
Dim matrix = New Integer(9, 9) {}
For i = 0 To matrix.GetUpperBound(0)
For j = 0 To matrix.GetUpperBound(1)
matrix(i, j) = rng.Next(1, 101)
Next
Next
Dim target = rng.Next(1, 101)
Dim matches As New List(Of String)
For i = 0 To matrix.GetUpperBound(0)
For j = 0 To matrix.GetUpperBound(1)
If matrix(i, j) = target Then
matches.Add($"({i},{j})")
End If
Next
Next
Console.WriteLine(If(matches.Any(),
$"{target} found at {String.Join(", ", matches)}",
$"{target} not found"))
uj5u.com熱心網友回復:
試試這個:
r = 0
c = txtbbus.Text
Dim i As Integer
Dim j As Integer
Dim FoundMatch As Boolean = False
For i = 0 To n - 1
For j = 0 To n - 1
If a(i, j) = c Then
FoundMatch = True
Exit For
End If
Next j
If FoundMatch = True Then
Exit For
End If
Next i
If FoundMatch = True Then
txtbres.Text = "The value exists " & r & " and it's in the position (" & i & ", " & j & ") "
Else
txtbres.Text = "The value doesn't exists"
End If
uj5u.com熱心網友回復:
我將假設c = txtbbus.Text是來自某種形式的輸入。意思是一個字串。對于相等性檢查,您將針對 Int 型別進行測驗。嘗試將輸入 from 轉換txtbbus.Text為整數。此外,就像另一位海報所說的那樣,打破回圈尋找你的匹配也是一個不錯的決定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/418013.html
標籤:
