2015-10-20 161 views
0

我在vb.net有一個項目來創建學生報告。我的主要問題是如何排列學生的分數列表,並顯示各個學生的分數和位置。 我如何排列學生的分數並顯示他們的名字,分數和位置? 這就是我所做的,它解決了問題的一部分。如何排名學生名單,並在vb.net中顯示他們的名字,分數和位置

該代碼如下。

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 

請幫忙嗎?

+0

沒有。它正在開展一個個人項目。能夠爲學生創建一個報告系統 –

+0

創建一個班級'組'將有所幫助。它應該有標題,分數和位置屬性。然後看看http://stackoverflow.com/questions/1301822/how-to-sort-an-array-of-object-by-a-specific-field-in-c比較這些項目。 –

+0

這是在C#中。如何將它與vb.net鏈接? –

回答

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) 
+0

如果您需要任何解釋請評論。 –

+0

好的,我們假設我想在文本框或標籤中輸出組2的標記,分數和位置,我該怎麼做? –

+0

查看更新的答案。 –