需要帮助在列表框中正确显示数组

时间:2017-02-02 02:17:18

标签: arrays vb.net sorting listbox

Public Class Form1
    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim Teams() As String = IO.File.ReadAllLines("SBWinners.txt")
        Dim Team As String
        Dim SteelersWins As Integer = 0
        Dim RaidersWins As Integer = 0
        Dim PackersWins As Integer = 0
        Dim CowboysWins As Integer = 0
        Dim GiantsWins As Integer = 0
        Dim RamsWins As Integer = 0
        Dim RavensWins As Integer = 0
        Dim SaintsWins As Integer = 0
        Dim FortyNinersWins As Integer = 0
        Dim RedskinsWins As Integer = 0
        Dim BroncosWins As Integer = 0
        Dim PatriotsWins As Integer = 0
        Dim ColtsWins As Integer = 0
        Dim DolphinsWins As Integer = 0
        Dim BearsWins As Integer = 0
        Dim JetsWins As Integer = 0
        Dim ChiefsWins As Integer = 0
        Dim BuccWins As Integer = 0

        For Each team In Teams
            If team = "Steelers" Then
                SteelersWins += 1
            End If
            If team = "Raiders" Then
                RaidersWins += 1
            End If
            If team = "Packers" Then
                PackersWins += 1
            End If
            If team = "Cowboys" Then
                CowboysWins += 1
            End If
            If Team = "Giants" Then
                GiantsWins += 1
            End If
            If team = "Rams" Then
                RamsWins += 1
            End If
            If team = "Ravens" Then
                RavensWins += 1
            End If
            If team = "Saints" Then
                SaintsWins += 1
            End If
            If team = "Forty-Niners" Then
                FortyNinersWins += 1
            End If
            If team = "Redskins" Then
                RedskinsWins += 1
            End If
            If team = "Broncos" Then
                BroncosWins += 1
            End If
            If team = "Patriots" Then
                PatriotsWins += 1
            End If
            If team = "Colts" Then
                ColtsWins += 1
            End If
            If team = "Dolphins" Then
                DolphinsWins += 1
            End If
            If team = "Bears" Then
                BearsWins += 1
            End If
            If team = "Jets" Then
                JetsWins += 1
            End If
            If Team = "Chiefs" Then
                ChiefsWins += 1
            End If
            If team = "Buccaneers" Then
                BuccWins += 1
            End If
        Next

        Dim Wins() As Integer = {SteelersWins, RaidersWins, PackersWins, CowboysWins, GiantsWins, RamsWins, RavensWins, SaintsWins, FortyNinersWins, RedskinsWins,
            BroncosWins, PatriotsWins, ColtsWins, DolphinsWins, BearsWins, JetsWins, ChiefsWins, BuccWins}

        For Each win In Wins
            Array.Sort(Wins)
            Array.Reverse(Wins)
            lstOutput.Items.Add(win)
        Next

    End Sub
End Class

我现在所拥有的代码是读取一个名为Superbowl获胜者的文本文件,并根据团队名称出现的次数计算获胜次数。然后将这些获胜放入数组中,并按降序排序并显示在列表框中。一切正常。

我的问题是我需要在列表框的同一行显示相应的团队名称及其获胜次数。所以,而不是:

6

5

5

4

4

需要:

6个钢人

5 49人

5名牛仔

4巨人

4包装工

等等。

1 个答案:

答案 0 :(得分:0)

您的代码存在一些问题,其中最重要的是您使用硬编码名称和变量来收集您的团队信息。如果添加新名称或更改名称,则会严重限制。

正如Plutonix在评论中提到的,您需要使用一个类来正确识别和整理您的信息。我还强烈建议您使用List(of T)集合来包含您的数据。

以下代码可以满足您的需求。

Private Class cls_Team_wins
    Public Team_Name As String
    Public Wins As Integer
    Public Sub New(Name As String)
        Team_Name = Name
        Wins = 1
    End Sub
    Public Overrides Function ToString() As String
        Return Strings.Right("  " & Wins, 2) & " - " & Team_Name
    End Function
End Class


Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim Games = New List(Of cls_Team_wins)
    Dim Teams() As String = IO.File.ReadAllLines("SBWinners.txt")

    For Each Team As String In Teams
        If Team <> "" Then
            Dim Team_Win As cls_Team_wins = Games.Find(Function(x) x.Team_Name = Team)
            If Team_Win Is Nothing Then
                Games.Add(New cls_Team_wins(Team))
            Else
                Team_Win.Wins += 1
            End If
        End If

    Next

    Games.Sort(Function(x, y) -x.Wins.CompareTo(y.Wins))

    ListBox1.DataSource = Games

End Sub

该课程包括团队名称和胜利计数器。它覆盖了ToString方法,因此列表框知道要显示为文本的内容。新功能要求您传递团队名称并将胜利计数器预设为一个。

For循环检查并忽略空白团队名称,这可能是读取文本文件的问题,尤其是最后一行。否则,如果它不存在,它将向列表中添加一个新团队,否则它将增加win计数器。

一旦构建了列表,它就会被反向排序(注意&#34中的减号;函数&#34;比较语句,这是一个反转排序的小技巧),最后被赋予列表框数据源。

祝你好运:)