題目
某次考試後,為了計算成績排名,這次考試科目有數學和程式設計兩科,其中以數學分數為主,當數學分數較高時,其名次亦較高;倘若數學同分,則再以程式設計的分數來決定名次高低;倘若數學和程式設計分數都一樣,依學號先後次序印出成績排名。
Module Module1
Public Class Student
Public Name As String
Public math, program As Integer
Public Sub New(ByVal name As String, ByVal math As Integer, ByVal program As Integer)
Me.Name = name
Me.math = math
Me.program = program
End Sub
End Class
Sub Main()
Dim Students As New List(Of Student) From {
New Student(1, 80, 85),
New Student(2, 90, 77),
New Student(3, 75, 75),
New Student(4, 75, 75),
New Student(5, 75, 80),
New Student(6, 85, 85),
New Student(7, 79, 82),
New Student(8, 78, 11) }
Dim Stud = From Student In Students
Order By Student.math Descending, Student.program Descending, Student.Name Ascending
Select Student
For Each sturec In Stud
Console.WriteLine(sturec.Name & " " & sturec.math & " " & sturec.program)
Next
Console.ReadLine()
End Sub
End Module
註: 加入list 另外一種寫法:
Dim Students As New List(Of Student)
Students.Add(New Student(1, 80, 85))
Students.Add(New Student(2, 90, 77))
Students.Add(New Student(3, 75, 75))
Students.Add(New Student(4, 75, 75))
Students.Add(New Student(5, 75, 80))
Students.Add(New Student(6, 85, 85))
Students.Add(New Student(7, 79, 82))
Students.Add(New Student(8, 78, 11))
註:select 可加入 條件 , 如數學 大於 80分。
Dim Stud = From Student In Students
where Student.math >80 Order By Student.math Descending, Student.program Descending, Student.Name Ascending
如果是字串,可用 like "李*" 此語法,即 姓名為 李 開頭的學生。