如何排名学生名单,并在vb.net中显示他们的名字,分数和位置

时间:2015-10-20 09:51:53

标签: vb.net multidimensional-array ranking

我在vb.net上有一个项目来创建学生报告。我的主要问题是如何对学生名单进行排名'分数和显示各种学生的#39;用他们的分数和位置命名。  我怎样才能让学生排名'分数并显示他们的名字,分数和位置? 这就是我所做的,它解决了部分问题。

守则如下。

Public Class Form1
Dim numbers(4) As Integer
Dim Group1Score, Group2Score, Group3Score, Group4Score, Group5Score, Group6Score As Integer

Dim Group1Title, Group2Title, Group3Title, Group4Title, Group5Title, Group6Title As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Group1Score = 4
    Group2Score = 8
    Group3Score = 15
    Group4Score = 16
    Group5Score = 34

    numbers(0) = Group1Score
    numbers(1) = Group2Score
    numbers(2) = Group3Score
    numbers(3) = Group4Score
    numbers(4) = Group5Score

    Array.Sort(numbers)
    Array.Reverse(numbers)

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label4.Text = numbers(0)
    Label5.Text = numbers(1)
    Label6.Text = numbers(2)
    Label7.Text = numbers(3)
    Label8.Text = numbers(4)

    'Next
End Sub
End Class

有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

为了更好地表示分数,我使用了ListBox。这是代码:

Public Class Form1

Dim GroupList As New List(Of Group)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    GroupList.Add(New Group("Group 1", 4))
    GroupList.Add(New Group("Group 2", 8))
    GroupList.Add(New Group("Group 3", 1))
    GroupList.Add(New Group("Group 4", 16))
    GroupList.Add(New Group("Group 5", 34))
    GroupList.Add(New Group("Group 6", 2))
    GroupList.Sort(New Comparison(Of Group)(Function(x, y) x.Score.CompareTo(y.Score)))
    GroupList.Reverse()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each item As Group In GroupList
        ListBox1.Items.Add(String.Format("{0}'s position is {1} with a score of {2}", item.Title, GroupList.IndexOf(item) + 1, item.Score))
    Next
End Sub
End Class

Public Class Group
    Public Title As String
    Public Score As Integer
    Public Sub New(_title As String, _score As Integer)
        Title = _title
        Score = _score
    End Sub
End Class

如果您想按名称或分数获取特定项目,可以使用List.Find方法。要按名称查找并显示其详细信息,请使用:

 Dim group = GroupList.Find(Function(item) item.Title = "Group 2")
 Dim position = GroupList.IndexOf(group) + 1
 Label1.Text = String.Format("Name: Group 2, Score: {0}, Position:{1}", group.Score, position)

答案 1 :(得分:0)

如果您想通过学生姓名访问信息,您可以使用学生姓名作为密钥的字典(https://msdn.microsoft.com/es-es/library/xfhwa508%28v=vs.110%29.aspx)和使用其余信息作为值的数组。