2015-10-07 61 views
0

我是初學者,需要學校項目的幫助。我試圖讓分數顯示從最高到最低和平均分數。我怎樣才能做到這一點?我目前的代碼是:VB上的算術測驗

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 

回答

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 
+0

你好。好吧,得分最高的是,當我點擊最高到最低的按鈕並且分數沒有顯示在正確的列中時,它在列表視圖中的名稱下隨機出現一個零。 – BradR