Private Type StdInfo '學生信息
Name As String
Score(5) As Integer '0~4為單科成績 5為總分
sn(5) As Integer '名次
End Type
Private Sub Form_Click()
Dim stds(9) As StdInfo, newstds() As StdInfo, nc As Integer
Dim s As String
Dim i As Integer
For i = 0 To 9
stds(i).Score(0) = 50 '此處隨機生成壹些分數 請自行修改實際分數
stds(i).Score(1) = 100 - i
stds(i).Score(2) = i + 10
stds(i).Score(3) = 100 - i
stds(i).Score(4) = i
stds(i).Name = CStr(i) '學生名字收錄 此處為"0"~"9"
Next
Do
s = InputBox("請分別輸入要查詢的同學名字,留空結束")
For i = 0 To 9
If stds(i).Name = s Then
ReDim Preserve newstds(nc)
newstds(nc).Name = stds(i).Name
newstds(nc).Score(0) = stds(i).Score(0)
newstds(nc).Score(1) = stds(i).Score(1)
newstds(nc).Score(2) = stds(i).Score(2)
newstds(nc).Score(3) = stds(i).Score(3)
newstds(nc).Score(4) = stds(i).Score(4)
newstds(nc).Score(5) = stds(i).Score(0) + stds(i).Score(1) + stds(i).Score(2) + stds(i).Score(3) + stds(i).Score(4)
'將最後壹個總分當作單科計算
nc = nc + 1
Exit For
End If
Next
Loop Until s = ""
Dim j As Integer, k As Integer
For k = 0 To 5 '科目
For i = 0 To nc - 1 '當前學生
For j = 0 To 9 '用於比較的學生
If newstds(i).Score(k) < stds(j).Score(k) Then
newstds(i).sn(k) = newstds(i).sn(k) + 1
'如果有分數比自己高的那麽自己這科名次+1
End If
Next
Next
Next
Print "名字", "成績1", "名次", "成績2", "名次", "成績3", "名次", "成績4", "名次", "成績5", "名次", "總分", "名次"
For i = 0 To nc - 1
Print newstds(i).Name,
For k = 0 To 5
Print newstds(i).Score(k), newstds(i).sn(k) + 1,
Next
Next
End Sub
==============第二次修改後的:==================
Option Explicit
Private Type StdInfo
Score() As Single '支持小數這次
ScoreNo() As Integer '名次
End Type
Dim Students() As StdInfo
Private Sub Form_Click()
Dim s As String, ScoreCount As Integer, c As Integer, sc As Single, m As Integer
s = InputBox("請輸入科目總數。")
If Trim(s) = "" Then Exit Sub
ScoreCount = CInt(s)
Do
sc = 0
c = c + 1
s = InputBox("請輸入第" & UBound(Students) + 1 & "個學生的第" & c & "/" & ScoreCount & "個科目的成績。留空結束")
If s <> "" Then
If UBound(Students(UBound(Students)).Score) <> ScoreCount + 1 Then
ReDim Preserve Students(UBound(Students)).Score(ScoreCount + 1)
ReDim Preserve Students(UBound(Students)).ScoreNo(ScoreCount + 1)
End If
Students(UBound(Students)).Score(c) = Val(s)
If c = ScoreCount Then
For m = 0 To c
sc = sc + Students(UBound(Students)).Score(m)
Next
Students(UBound(Students)).Score(c + 1) = sc
ReDim Preserve Students(UBound(Students) + 1)
ReDim Students(UBound(Students)).Score(ScoreCount + 1)
ReDim Students(UBound(Students)).ScoreNo(ScoreCount + 1)
c = 0
End If
End If
Loop Until s = ""
Dim i As Integer, j As Integer, k As Integer
For k = 1 To ScoreCount + 1 '科目
For i = 0 To UBound(Students) - 1 '當前學生
For j = 0 To UBound(Students) - 1 '用於比較的學生
If Students(i).Score(k) < Students(j).Score(k) Then
Students(i).ScoreNo(k) = Students(i).ScoreNo(k) + 1
End If
Next
Next
Next
Print "學生",
For j = 1 To ScoreCount
Print "科目"; CStr(j); "分數", "名次",
Next
Print "總分", "名次"
For i = 0 To UBound(Students) - 1
Print CStr(i) + 1,
For j = 1 To ScoreCount + 1
Print CStr(Students(i).Score(j)), CStr(Students(i).ScoreNo(j) + 1),
Next
Next
End Sub
Private Sub Form_Load()
ReDim Students(0)
ReDim Students(0).Score(0)
ReDim Students(0).ScoreNo(0)
End Sub