VB学生成绩排序 创建两个数组,一个数组放入学生姓名,另一数组放入学生的语文成绩,请录入10个学生姓名和

创建两个数组,一个数组放入学生姓名,另一数组放入学生的语文成绩,请录入10个学生姓名和成绩,在文本框中输入学生姓名,就可查询学生成绩。根据成绩给学生排序。按成绩从低到高输出学生姓名。

Public Class Form1
Dim Names(10) As String
Dim Scores(10) As Integer

'查询,返回-1表示未找到该学生
Public Function GetScoreByName(ByVal name As String) As Integer
For i As Integer = 0 To 9
If name = Names(i) Then
Return Scores(i)
End If
Next
Return -1
End Function

'排序,输出名字和分数,格式:"1.张三:80"
Public Function GetNamesSortByScore() As String
For i As Integer = 0 To 9
For j As Integer = i + 1 To 9
If Scores(i) < Scores(j) Then
Dim temp1 As Integer = Scores(i)
Dim temp2 As String = Names(i)
Scores(i) = Scores(j)
Names(i) = Names(j)
Scores(j) = temp1
Names(j) = temp2
End If
Next
Next
Dim returnstring As String = ""
For index As Integer = 0 To 9
returnstring = returnstring & (index + 1).ToString() & "." & Names(index) & ":" & Scores(i).tostring() & vbCrLf
Next
Return returnstring
End Function
End Class
温馨提示:答案为网友推荐,仅供参考
相似回答