我正在尝试在加载到数组中的文件中排序最大的分数。当前,程序打开文件,然后读取文件,然后将每一行分为两部分-名称和乐谱;然后将其存储在数组中。我不确定如何对数组进行排序以找到最大的10分并将其放入列表框。目前,程序会找到大于0的任何分数并将其放在列表框中
Dim FileNum As Integer = FreeFile()
FileOpen(FileNum, "GameResultsFile", OpenMode.Input)
For index = 0 To 99
Dim temp() As String = LineInput(FileNum).Split(",") 'CUTTING LINE INTO TWO SECTIONS
MemoryGame.HighScores(index).Name = temp(0) 'NAME (First Part of Line)
MemoryGame.HighScores(index).Score = temp(1) 'SCORE (Second Part of Line)
If temp(1) > 0 Then 'If any of the scores is above 0 then
ListBox1.Items.Add(temp(0) + " " + temp(1)) ' display the name of the person who got that score and their score
End If
Next
FileClose()
答案 0 :(得分:1)
我会用IComparable来做这样的事情
首先,我将像这样从您的文本文件加载数据,并将数据保存到List Of,而Type将是下面的Player
类。
'' Get Data From Text File And Create An Anonymous Type
Private Sub LoadPlayerAndScore(path As String)
'' Load data from text file
Dim data = From line In System.IO.File.ReadAllLines(path)
Let val = line.Split(",")
Select New With {Key .Name = val(0), Key .Score = val(1)}
'' Save data to list
For Each pair In data
Dim player As New Player With {
.Name = pair.Name,
.Score = pair.Score
}
playersList.Add(player)
Next
End Sub
然后我继续创建一个播放器类,该类将实现上面列出的ICompareble。
Class Player
Implements IComparable(Of Player)
Public Property Name As String
Public Property Score As Integer
Public Sub Player(ByVal name As String, ByVal score As Integer)
Me.Name = name
Me.Score = score
End Sub
'' Sort Player From The Hightest Score To The Lowest Score
Private Function IComparable_CompareTo(other As Player) As Integer Implements IComparable(Of Player).CompareTo
Return other.Score.CompareTo(Me.Score)
End Function
End Class
然后我将创建一些公共变量,例如
Dim playersList As New List(Of Player)
Dim fileLocation As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Settings.txt")
将路径更改为文件的位置。
最后在Form Load Event
中,我会这样称呼它
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'' Load Data From Text File On Desktop
LoadPlayerAndScore(path:=fileLocation)
'' Sort List
playersList.Sort()
'' Add Values To List
For Each p As Player In playersList
ListBox1.Items.Add("Name: " + p.Name + " Score: " + p.Score.ToString())
Next
End Sub
这是代码的整体外观
Public Class Form1
Dim playersList As New List(Of Player)
Dim fileLocation As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Settings.txt")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'' Load Data From Text File On Desktop
LoadPlayerAndScore(path:=fileLocation)
'' Sort List
playersList.Sort()
'' Add Values To List
For Each p As Player In playersList
ListBox1.Items.Add("Name: " + p.Name + " Score: " + p.Score.ToString())
Next
End Sub
'' Get Data From Text File And Create An Anonymous Type
Private Sub LoadPlayerAndScore(path As String)
'' Load data from text file
Dim data = From line In System.IO.File.ReadAllLines(path)
Let val = line.Split(",")
Select New With {Key .Name = val(0), Key .Score = val(1)}
'' Save data to list
For Each pair In data
Dim player As New Player With {
.Name = pair.Name,
.Score = pair.Score
}
playersList.Add(player)
Next
End Sub
End Class
Class Player
Implements IComparable(Of Player)
Public Property Name As String
Public Property Score As Integer
Public Sub Player(ByVal name As String, ByVal score As Integer)
Me.Name = name
Me.Score = score
End Sub
'' Sort Player From The Hightest Score To The Lowest Score
Private Function IComparable_CompareTo(other As Player) As Integer Implements IComparable(Of Player).CompareTo
Return other.Score.CompareTo(Me.Score)
End Function
End Class
这是我得到的输出
这是文本文件数据的外观。
ANDREW,25
MERVE,12
RUZGAR,50
对于前十名的人,请遵循上面的评论。
答案 1 :(得分:1)
如何尝试?
Dim sortedArray = _
File _
.ReadAllLines("GameResultsFile") _
.Select(Function (line) line.Split(","c)) _
.Select(Function (parts) New With { .Name = parts(0), .Score = Integer.Parse(parts(1)) }) _
.OrderByDescending(Function (x) x.Score) _
.Select(Function (x) x.Name & " " & x.Score)
.ToArray()
For Each item As String In sortedArray
ListBox1.Items.Add(item)
Next
答案 2 :(得分:1)
在线评论和解释
'Note Imports System.IO
Structure Player
Public Score As Integer
Public Name As String
'Added a constructor to the structure to make it easy to add new Player
Public Sub New(myScore As Integer, myName As String)
Score = myScore
Name = myName
End Sub
End Structure
Private HighScores(99) As Player
Private index As Integer 'used in both LoadArray and SortAndDisplayArray
Private Sub LoadArray()
Using sr As New StreamReader("GameResultsFile.txt")
Dim line As String
Do While sr.Peek() > -1 'Peek checks if there is another character in the file
line = sr.ReadLine()
Dim temp() As String = line.Split(","c) 'CUTTING LINE INTO TWO SECTIONS
'Notice the elements of the temp array are switched to match the
'Player constructor (Sub New)
HighScores(index) = New Player(CInt(temp(1)), temp(0))
index += 1 'not only keeps track of the index but remembers how many elements
'we have added to HighScores
Loop
End Using
End Sub
Private Sub SortAndDisplayArray()
'This is the LINQ way to do it, you can do a great deal in one line of code
'There is a loop underneath but you don't have to write it.
'I added a Take clause so we will not get a bunch of 0- in the list box going up to index 99
' You might want to show, for example only the top ten scorers, so change to Take 10
Dim orderArray = From scorer In HighScores Order By scorer.Score Descending Select $"{scorer.Score} - {scorer.Name}" Take index
ListBox1.DataSource = orderArray.ToList
End Sub
我仍然认为List(Of T)会更容易,但是我感觉到您的分配要求您使用数组。