Arrays VB数组错误等级书赋值
我的问题是,当我输入四个测试的分数时:1 2 3 3,它计算出所有测试的总数和平均值,但如果我输入大于3的测试分数,这比我的数组大,它会在这个函数中给我错误Arrays VB数组错误等级书赋值,arrays,vb.net,Arrays,Vb.net,我的问题是,当我输入四个测试的分数时:1 2 3 3,它计算出所有测试的总数和平均值,但如果我输入大于3的测试分数,这比我的数组大,它会在这个函数中给我错误 Public Function TotalScore(studentScore() As Integer, intTotalScore As Integer) As Integer For Each i As Integer In studentScore intTotalScore += studentScore(
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
错误消息是索引超出数组范围。
我很抱歉,我不知道如何更好地解释它,如果你需要额外的代码或一些更多的细节,我将非常乐意提供。谢谢你抽出时间
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次测试的总分
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
该错误意味着您正试图从数组索引中获取数组中不存在的内容。例如,数组有10个从0到9索引的元素,您调用的是索引号11
请尝试对每个对象使用以下命令:
For Each i As Integer In studentScore
intTotalScore += i
Next
For Each i As Integer In studentScore
intTotalScore += i
Next