2
我的老師已經指示我們的課程在Visual Basic中創建一個基本的詞語排序程序'老式的方式'。因此,比較兩個數組值a和b,如果其中一個被認爲是高於另一個的順序,則交換它們,如果不是什麼都不做,繼續直到沒有更多的交換。這裏是我到目前爲止的代碼:使用VB.net的基本名稱排序程序
Imports System.IO
Imports System
Public Class Form1
Public arrText As New ArrayList()
Private Sub btnImprt_Click(sender As Object, e As EventArgs) Handles btnImprt.Click
'Dim OpenAnswerFile As New OpenFileDialog
Dim objReader As New StreamReader("c:\Users\Adam\Desktop\unSortList.txt")
Dim sLine As String = ""
Dim arrText As New ArrayList()
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()
Dim i As Integer = 0
txtImport.Text = arrText(i)
End Sub
Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
Dim i As Integer = 0
Dim a As Integer = i + 1
txtImport.Text = i
txtImport.Text = a
Dim Temp As String
Dim Change As Boolean = True
While Change = True
Change = False
For Each i In arrText(i) - 1
If String.Compare(arrText(i), arrText(i + 1)) = 1 Then
Change = True
Temp = arrText(i)
arrText(i) = arrText(i + 1)
arrText(i + 1) = Temp
End If
Next
i = 0
End While
txtSort.Text = arrText(39)
End Sub
我的問題是,我得到一個指數錯誤,我不知道在哪裏的錯誤所在的邏輯似乎罰款。
是的,我知道內置到Visual Basic中的排序函數。但正如老師所說的那樣。別作弊。
如果您正在使用調試器來運行你的工作會容易得多(和你應該是,除非特別禁止)。如果您使用的是Visual Studio,請在程序開始時添加一個斷點,然後使用F11或「調試」菜單進入代碼。 –
我不打算給你答案,但有人擡頭,你有arrText在類級別和方法級別聲明,在btnSort方法中失去聲明,第二次使用For循環,因爲你不能修改在For Each循環中收集數據,因爲數組中的第三個數據基於.net,所以通常需要迭代到數組長度減1,但由於您還在比較下一個值,因此需要迭代到長度爲-2 –
所以我清理了我糟糕的代碼並在這裏實現了一些提示annnnddd ......它工作正常!雖然當它在第二個富文本框中輸出答案時,大約有12行的空白區域,所以我可以看看問題出在哪裏。感謝你們! – FuzzyPanda