VB上的算术测验

时间:2015-10-07 13:49:53

标签: vb.net

我是初学者,需要学校项目的帮助。我试图让分数显示从最高到最低以及平均分数。我怎样才能做到这一点?我目前的代码是:

Dim filename As String
        Dim numberofrecords As Short
        Dim countofloop As Short
        Dim one, two, three As Short

        filename = "N:\Class1.dat"
        FileOpen(1, filename, OpenMode.Random, , , Len(OneStudentScore))
        numberofrecords = LOF(1) / Len(OneStudentScore)

        countofloop = 0

        Dim scores(numberofrecords) As Integer
        Dim names(numberofrecords) As String

        Do While (Not EOF(1))
            FileGet(1, OneStudentScore)

            one = OneStudentScore.score1
            two = OneStudentScore.score2
            three = OneStudentScore.score3

            names(countofloop) = OneStudentScore.Name

            If one > two Then
                If one > three Then
                    '1st score is the biggest
                    scores(countofloop) = one
                Else
                    scores(countofloop) = three
                End If
            Else
                If two > three Then
                    scores(countofloop) = two
                Else
                    scores(countofloop) = three
                End If
            End If

            countofloop = countofloop + 1
        Loop

        'sort array into score order
        Array.Sort(scores, names)

        'this allows the arrays to be in descending order - highest first 
        Array.Reverse(scores)
        Array.Reverse(names)

        Dim displayeditem As String
        lstClass1.Items.Clear()

        'display the arrays in a list box
        For i = 0 To countofloop
            displayeditem = names(i) & " " & scores(i)
            lstClass1.Items.Add(displayeditem)

            FileClose()
        Next

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我没有运行代码,但看起来您正在对数组进行正确排序,从最高到最低。如果你只需要平均值那么它应该只是将scores数组中的元素相加并除以元素的数量。类似的东西:

Dim total As Integer = 0
Dim numScores As Integer = 0
For i As Integer = 0 To scores.Length - 1
    total += scores(i)
    numScores += 1
Next

Dim average As Integer
average = total / numScores