数组nam()、sex()、math()、su()分别存放学生的姓名、性别、数学和总分成绩,现要求编写一个程序使得数据按照男生“总分”的降序和女生“总分”的升序排列,其中男生在前女生在后,程序界面如下图所示:
实现该功能的VB程序如下,但加框处代码有错,请改正。
Dim nam(100) As String '存放学生姓名
Dim sex(100) As String '存放学生性别
Dim math(100) As Long '存放学生数学成绩
Dim su(100) As Long '存放学生总分成绩
Dim k As Integer 'k变量用来存学生人数
Private Sub Form_Load()
‘将数据库中的数据分别存储在相应的变量中并在列表框中显示出来
‘代码略
End Sub
Private Sub Command1_Click()
Dim mmax As Integer, gmax As Integer, flagm As Boolean, flagg As Boolean
i = 1: flagm = True: flagg = True
Do While i <= k And flagm = True ①
mmax = 0: gmax = 0
flagm = False: flagg = False
For j = i To k - i + 1
If sex(j) = "男" And su(j) > su(mmax) Then mmax = j
If sex(j) = "女" And su(j) > su(gmax) Then gmax = j
Next j
If mmax <> 0 Then
flagm = True
t = nam(mmax): nam(mmax) = nam(i): nam(i) = t
t = sex(mmax): sex(mmax) = sex(i): sex(i) = t
t = math(mmax): math(mmax) = math(i): math(i) = t
t = su(mmax): su(mmax) = su(i): su(i) = t
End If
If gmax <> 0 Then
flagg = True
If j = mmax Then ②
gmax = mmax
t = nam(gmax): nam(gmax) = nam(k - i + 1): nam(k - i + 1) = t
t = sex(gmax): sex(gmax) = sex(k - i + 1): sex(k - i + 1) = t
t = math(gmax): math(gmax) = math(i): math(i) = t
t = su(gmax): su(gmax) = su(k - i + 1): su(k - i + 1) = t
Else
t = nam(gmax): nam(gmax) = nam(k - i + 1): nam(k - i + 1) = t
t = sex(gmax): sex(gmax) = sex(k - i + 1): sex(k - i + 1) = t
t = math(gmax): math(gmax) = math(k - i + 1): math(k - i + 1) = t
t = su(gmax): su(gmax) = su(k - i + 1): su(k - i + 1) = t
End If
End If
i = i + 1
Loop
For i = 1 To k
List2.AddItem nam(i) + " " + sex(i) + " " + Str(math(i)) + " " + Str(su(i))
Next i
End Sub
程序中①处改为{#blank#}1{#/blank#}。
程序中②处改为{#blank#}2{#/blank#}。