首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

怎么排名次?救阿

2011-12-29 
如何排名次?急救阿现在有5个人打了5个分数,要根据它的大小进行排名次,然后将名次与分数显示出来,如何做啊?

如何排名次?急救阿
现在有5个人打了5个分数,要根据它的大小进行排名次,然后将名次与分数显示出来,如何做啊?

[解决办法]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

Dim DGV As New DataGridView
DGV.Dock = DockStyle.Top
DGV.Columns.Add("序号Column", "序号")
DGV.Columns.Add("成绩Column", "成绩")
DGV.Columns.Add("名次Column", "名次")
Dim 成绩() As Integer
成绩 = New Integer() {20, 34, 18, 34, 29}
Dim i As Integer

DGV.Columns("序号Column").ValueType = i.GetType
DGV.Columns("成绩Column").ValueType = 成绩.GetType
DGV.Columns("名次Column").ValueType = i.GetType

For i = 0 To 成绩.Length - 1
DGV.Rows.Add()
DGV.Rows(i).Cells("序号Column").Value = i + 1
DGV.Rows(i).Cells("成绩Column").Value = 成绩(i)

Next

DGV.Sort(DGV.Columns("成绩Column"), System.ComponentModel.ListSortDirection.Descending)

Dim 名次 = 1
Dim bak成绩 = DGV.Rows(0).Cells("成绩Column").Value
For i = 0 To 成绩.Length - 1
If DGV.Rows(i).Cells("成绩Column").Value <> bak成绩 Then
名次 = i + 1
End If
DGV.Rows(i).Cells("名次Column").Value = 名次
bak成绩 = DGV.Rows(i).Cells("成绩Column").Value
Next


Me.Controls.Add(DGV)
End Sub
[解决办法]
Private Sub Sort()
Dim Intjyouretu(5) As Integer
Intjyouretu(0) = 8
Intjyouretu(1) = 7
Intjyouretu(2) = 3
Intjyouretu(3) = 6
Intjyouretu(4) = 5
Dim i As Integer
Dim j As Integer
Dim k As Integer
For i = 0 To 4
For j = i + 1 To 4
If Intjyouretu(j) > Intjyouretu(i) Then
k = Intjyouretu(i)
Intjyouretu(i) = Intjyouretu(j)
Intjyouretu(j) = k

End If
Next
Next

MsgBox(Intjyouretu(0) & Intjyouretu(1) & Intjyouretu(2) & Intjyouretu(3) & Intjyouretu(4))
End Sub
[解决办法]

探讨
引用:
冒泡排序

[解决办法]
Private Sub Sort()
Dim arr As New ArrayList
arr.Add(89)
arr.Add(82)
arr.Add(49)
arr.Add(79)
arr.Add(39)
arr.Sort() '升序
arr.Reverse(0, 5) '降序
MsgBox(arr(0) & arr(1) & arr(2) & arr(3) & arr(4))

End Sub
[解决办法]
参考如下代码:
VB.NET code
        Dim sngScore(4) As Single        sngScore(0) = 95        sngScore(1) = 98        sngScore(2) = 97        sngScore(3) = 99.5        sngScore(4) = 95        Dim intI As Integer        Dim sngTemp As Single        Dim intIndex As Integer '   名次        Dim strRtn As String        Array.Sort(sngScore)        Array.Reverse(sngScore)        sngTemp = -1        For intI = 0 To sngScore.Length - 1            If sngTemp <> sngScore(intI) Then                intIndex = intIndex + 1            End If            sngTemp = sngScore(intI)            strRtn = strRtn & "第" & intIndex.ToString & "名:" & sngScore(intI).ToString & vbCrLf        Next        MsgBox(strRtn) 

热点排行