For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
For valueWithinArray As Integer = 0 To arrayToSort.Length - 2
If arrayToSort(arrayValue) > arrayToSort(arrayValue 1) Then
Dim tempStorage As Integer = arrayToSort(arrayValue)
arrayToSort(arrayValue) = arrayToSort(arrayValue 1)
arrayToSort(arrayValue 1) = tempStorage
End If
Next
Next
陣列宣告為arrayToSort(7),是從 9 到 2 的整數。
uj5u.com熱心網友回復:
我將您的變數名稱更改valueWithinArray為index. 它實際上不是陣列中的值,而是元素的索引。您arrayValue在您真正想要的地方valueWithinArray(現在稱為index)引入了另一個未宣告的變數 ( )。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim arrayToSort() As Integer = {6, 8, 7, 2, 9, 4, 5, 3}
For numberOfRunsCompleted As Integer = 0 To arrayToSort.Length
For index As Integer = 0 To arrayToSort.Length - 2
If arrayToSort(index) > arrayToSort(index 1) Then
Dim tempStorage As Integer = arrayToSort(index)
arrayToSort(index) = arrayToSort(index 1)
arrayToSort(index 1) = tempStorage
End If
Next
Next
For Each i In arrayToSort
Debug.Print(i.ToString)
Next
End Sub
這是一個很好的學習練習,但為了節省一些打字...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim arrayToSort() As Integer = {9, 8, 7, 6, 5, 4, 3, 2}
Array.Sort(arrayToSort)
For Each i In arrayToSort
Debug.Print(i.ToString)
Next
End Sub
結果將顯示在立即視窗中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/320900.html
標籤:网络
