0

私の問題は、4 つのテストのスコアを入力するときです: 1 2 3 3、合計と平均を計算します。

Public Function TotalScore(studentScore() As Integer, intTotalScore As Integer) As Integer
    For Each i As Integer In studentScore
        intTotalScore += studentScore(i)
    Next
    Return intTotalScore
End Function 

エラーメッセージはそれindex is out of range arrayです。申し訳ありませんが、追加のコードや詳細が必要な場合は、より適切に説明する方法がわかりません。喜んで提供させていただきます。お時間をいただきありがとうございます

 Public g_intTotalScore As Integer                                                       'total score
 Public g_decAverageScore As Decimal                                                     'average score
 Public g_strLetterScore As String
 Public Const intMAX_SUBSCRIPT_STUDENTS_NAMES As Integer = 4                            'max subscript for students names 
 Public Const intMAX_SUBSCRIPT_SCORE As Integer = 3                                      'max subscript for student numeric scores on 4 test

Public strStudentsNames(intMAX_SUBSCRIPT_STUDENTS_NAMES) As String                 'array that holds students names
Public strLetterGrades() As String = {"A", "B", "C", "D", "F"}                     'array that hold letter grades
Public intStudent1(intMAX_SUBSCRIPT_SCORE) As Integer                              'hold test scores for student1

4 つのテストの合計スコアを計算する 2 つのパラメーターを受け入れる関数を作成します。

Public Function TotalScore(studentScore() As Integer, intTotalScore As Integer) As Integer
    For Each i As Integer In studentScore
        intTotalScore += studentScore(i)
    Next
    Return intTotalScore
End Function

ここでは、学生名と 4 つのテストの点数を入力します。

Do While intCount < strStudentsNames.Length
    'input data for student number1
    If intCount = 0 Then
        intCounter = 0
        strStudentsNames(intCount) = InputBox("Enter Student Name number" &       intCount + 1, "Enter Data")
        Do While intCounter < intStudent1.Length
            intStudent1(intCounter) = CInt(InputBox("Student Name: " & strStudentsNames(intCount) & vbCrLf &
                                                    "Enter Score for test number " & intCounter + 1, "Enter Data"))
            intCounter += 1
        Loop
    End If
    intCount += 1
Loop

ここにリストする表示データがあります

'student1: calculate total , avaerage score, display average score, reset total score
g_intTotalScore = TotalScore(intStudent1, g_intTotalScore)
g_decAverageScore = Average(g_decAverageScore)
lstOutPut.Items.Add("Student Name: " & strStudentsNames(0) & " => The average score is: " & g_decAverageScore.ToString & " => Grade: ")
g_intTotalScore = 0
4

1 に答える 1