Option Explicit
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
Print
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
Print
Next
End Sub
Private Sub Form_Load()
ReDim Students(0)
ReDim Students(0).Score(0)
ReDim Students(0).ScoreNo(0)
End Sub
温馨提示:内容为网友见解,仅供参考