Vb.net 将二维数组列数据添加到一起?

Vb.net 将二维数组列数据添加到一起?,vb.net,arrays,2d,Vb.net,Arrays,2d,如果你能给我指出正确的方向,那就太好了。我试着研究这个问题,但没有找到解决办法 我有一个2d数组,其中包含学生姓名和3个年级(英语、数学、科学) 学生数组(24,3) 现在,我想使用数组中保存的数据来创建平均值 我需要一个班级平均分:数学、英语和科学。我还需要所有科目的总体平均分 到目前为止,我使用的代码带来了一些奇怪的结果 studentarray(i,1) / count studentarray(i,2) / count studentarray(i,3) / count total

如果你能给我指出正确的方向,那就太好了。我试着研究这个问题,但没有找到解决办法

我有一个2d数组,其中包含学生姓名和3个年级(英语、数学、科学)

学生数组(24,3)

现在,我想使用数组中保存的数据来创建平均值

我需要一个班级平均分:数学、英语和科学。我还需要所有科目的总体平均分

到目前为止,我使用的代码带来了一些奇怪的结果

studentarray(i,1) / count 
studentarray(i,2) / count

studentarray(i,3) / count

totalaverage = (studentarray(i,1) + studentarray(i,2) + studentarray(i,3)) / count
前3个平均值仅给出数组中第一个条目的结果。然后计算将只显示3列中显示的数据,例如101010

任何帮助都将不胜感激

Function CalcSubjectAverage(ByVal studentArray(,) As Integer, ByVal subject As Integer) As Single
    Dim total As Single
    For i As Integer = 0 To studentArray.Length
        total += studentArray(i, 0)
    Next

    Return total / studentArray.Length
End Function

你需要先把受试者加起来,然后你才能计算出平均数:)

我希望这一项也能有所帮助:

我在守则中提供了必要的意见

代码: '这是二维动态数组 '用于存储学生成绩数据的学生阵列(,) '每个主题的平均值(,)用于存储平均值 “每个学生的价值观() '实际上,您只能使用第一个数组,但为了使代码更易于理解_ '我们声明另外两个数组 Dim StudentArray(,),Ave_Values_per_Subject(,),Ave_Values_per_Student()作为对象 这就是功能 次级解决问题(ByVal Num_of_Students作为单位积分,ByVal Num_of_Students作为单位积分)


如果我的答案对您有帮助,请不要忘记按复选标记按钮将其标记为已回答:)
    ' get the number of students and resize the array
    ReDim StudentArray(Num_of_Students - 1, Num_of_Subjects - 1), Ave_Values_per_Subject(0, Num_of_Subjects - 1), _
          Ave_Values_per_Student(Num_of_Students - 1)
    ' you can imagine this as having a table with Num_of_Students as the number of rows and Num_of_Subjects as the _
    ' number of columns
    ' StudentArray(0,0) gives the value of student #1 at subject #1 (say english) _
    ' StudentArray(0,1) gives the value of student #1 at subject #2 (say math) _
    ' StudentArray(1,3) gives the value of student #2 at subject #3 (say science) and so on

    ' example: we have 4 students with english, math, and science as subjects
    ' thus Num_of_Students = 4 and Num_of_Subjects = 3 giving us StudentArray(3,2) and Ave_Values_per_Subject(0,2)
    ' Suppose the grades of student #1 for english, math, and science are 70, 80, and 90 respectively; _
    ' student #2 = {75, 80, 90}; student #3 = {75, 85, 95}; and student #4 = {60, 100, 85}
    ' Suppose index 0 of StudentArray (StudentArray(0,0)) represents english subject; _
    ' StudentArray (0,1) = math; and StudentArray (0,2) = science

    '' to calculate for the average of students for EACH subject and to store it in a separate array:
    For subjectCount = 0 To UBound(StudentArray, 2)
        Dim SumPerSubject As Single
        For studentCount = 0 To UBound(StudentArray, 1)
            SumPerSubject += StudentArray(studentCount, subjectCount)
        Next
        ' average of students per subject:
        Ave_Values_per_Subject(0, subjectCount) = SumPerSubject / (UBound(StudentArray, 1) + 1)
        ' the average of students per subject is determined and stored in the above array
        ' this means that the average of values for english is stored in Ave_Values_per_Subject(0,0) _
        ' Ave_Values_per_Subject(0,1) for math; and Ave_Values_per_Subject(0,2) for science
    Next

    '' to calculate for the average of EACH student on all subjects:
    For studentCount = 0 To UBound(StudentArray, 1)
        Dim SumPerStudent As Single
        For subjectCount = 0 To UBound(StudentArray, 2)
            SumPerStudent += StudentArray(studentCount, subjectCount)
        Next
        ' ave values of each student on all subjects:
        Ave_Values_per_Student(studentCount) = SumPerStudent / 3
    Next
End Sub