[英]Visual Basic Sorting an Array with Data Read in from Text File
我遇到这个问题:
btnDisplay_Click过程应该读取states.txt文件中包含的五个名称,将每个名称存储在一个五元素的一维数组中。 该过程应按降序对数组进行排序,然后在列表框中显示该数组的内容。
使用我的代码,我可以在列表框中显示5个州名,但是它们没有被排序。
代码的第一次迭代(旧):
Public Class frmMain
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'Declare an array for 5 states
Dim strStates(4) As String
Dim strStateName As String
'Sort the array in descending order
Array.Sort(strStates)
Array.Reverse(strStates)
'Declare variable to hold stream reader object
Dim inFile As IO.StreamReader
'Check if txt file exists before opening to avoid run time error/crash
If IO.File.Exists("states.txt") Then
'Open the file
inFile = IO.File.OpenText("states.txt")
'Loop instructions until end of file is reached
Do Until inFile.Peek = -1
'Read a line
strStateName = inFile.ReadLine
'Add line (state) to list box
lstNames.Items.Add(strStateName)
Loop
'Close the file
inFile.Close()
Else
'Show a message box telling user file can't be found
MessageBox.Show("File does not exist or cannot be found.", "States", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
我也尝试将排序线放在循环中。 如何在列表框中显示已排序的数组?
代码的第二次迭代(最新):
Public Class frmMain
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'Declare an array to hold all 5 states
Dim strStates(4) As String
'Declare variable to hold loop counts
Dim i As Integer = 0
'Declare variable to hold stream reader object
Dim inFile As IO.StreamReader
'Check if txt file exists before opening to avoid run time error/crash
If IO.File.Exists("states.txt") Then
'Open the file
inFile = IO.File.OpenText("states.txt")
'Loop instructions until end of file is reached
Do Until inFile.Peek = -1
'Read a line and store in array
strStates(i) = inFile.ReadLine
'Message box to confirm array loop is working correctly
MessageBox.Show(strStates(i))
'Manually increment array counter
i = i + 1
Loop
'Close the file
inFile.Close()
'Sort the array in descending order
Array.Sort(strStates)
Array.Reverse(strStates)
'Output to list box
lstNames.Items.Add(strStates(i)) 'error thrown here
Else
'Show a message box telling user file can't be found
MessageBox.Show("File does not exist or cannot be found.", "States", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
更新If语句:
If IO.File.Exists("states.txt") Then
'Open the file
inFile = IO.File.OpenText("states.txt")
'Loop instructions until end of file is reached
Do Until inFile.Peek = -1
'Read a line and store in array
strStates(i) = inFile.ReadLine
'Manually increment array counter
i = i + 1
Loop
'Close the file
inFile.Close()
'Sort the array in descending order (Next 2 lines don't work)
Array.Sort(strStates)
Array.Reverse(strStates)
'Output to list box
lstNames.Items.Add(strStates(i))
Else
'Show a message box telling user file can't be found
MessageBox.Show("File does not exist or cannot be found.", "States", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
我得到了,感谢你们的帮助! 对于我的上一期,我刚补充说:
For i = 0 To strStates.Length - 1
lstNames.Items.Add(strStates(i))
Next i
所以最终的工作代码如下所示:
Public Class frmMain
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
'Declare an array to hold all 5 states
Dim strStates(4) As String
'Declare variable to hold loop counts
Dim i As Integer = 0
'Declare variable to hold stream reader object
Dim inFile As IO.StreamReader
'Check if txt file exists before opening to avoid run time error/crash
If IO.File.Exists("states.txt") Then
'Open the file
inFile = IO.File.OpenText("states.txt")
'Loop instructions until end of file is reached
Do Until inFile.Peek = -1
'Read a line and store in array
strStates(i) = inFile.ReadLine
'Message box to confirm array loop is working correctly
'MessageBox.Show(strStates(i))
'Manually increment array counter
i = i + 1
Loop
'Close the file
inFile.Close()
'Sort the array in descending order
Array.Sort(strStates)
Array.Reverse(strStates)
'Output to list box
For i = 0 To strStates.Length - 1
lstNames.Items.Add(strStates(i))
Next i
Else
'Show a message box telling user file can't be found
MessageBox.Show("File does not exist or cannot be found.", "States",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
多谢你们!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.