Grade Array Averager麻烦

时间:2014-06-08 23:56:21

标签: arrays vb.net visual-studio-2010

我目前正在大学攻读IT课程。然而,Visual Basic 2010是一项要求,我不是程序员。我一直在努力寻找通过VB的方法,但最后的任务让我感到困惑。我能够获得数组中的第一个名称和该名称的5个等级。此时,循环将继续询问下一个名称,并命名5个等级,依此类推,直到输入第4个名称和等级,然后它应显示列表框中的所有4个名称和等级平均值。

这是作业......

编写一个程序,为每个学生输入四个学生的名字和平均五个考试成绩。该程序应该有一个学生姓名的数组,然后是所有成绩的二维数组。 您的课程应该询问学生姓名,然后询问该学生的五个考试成绩。 创建一个执行平均并将数组传递给该方法的方法。该方法还可以在列表框中输出学生姓名和平均值。 一旦获得所有成绩,请调用一种方法来计算平均值。当你得到信息时,不要搞清楚!如果你这样做,你会得到一个大的零!然后使用相同的方法将结果输出到列表框中:

经过4天的努力,到目前为止我已经提出了这个问题。非常感谢任何指导。先感谢您。

Public Class Form1

    Private Sub btnNames_Click(sender As System.Object, e As System.EventArgs) Handles btnNames.Click
        Dim NamesList(3) As String

        Dim GradeArray(4) As Integer
        Dim x As Integer
        Dim y As Integer
        Dim Sum As Integer
        Dim Avg As Integer

        For y = 0 To NamesList(3)
            NamesList(x) = InputBox("Enter student number " & y + 1 & "'s name:", "Enter a name")
        Next

        For y = 0 To GradeArray.Length - 1
            GradeArray(y) = InputBox("Enter grade number " & y + 1 & " for " & NamesList(0) & " in the box:", "Enter the grades")
        Next

        For Each item In GradeArray
            Sum = Sum + item
        Next

        Avg = Sum / 5

        lstAverages.Text = Avg.ToString

    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

我没有其他更好的事情可做,所以我尝试了一下......还包括按照你的说法:4名学生 - 每个5个等级,学生姓名数组和2D数组用于保存所有成绩。有一种方法可以将这些传递给它并对学生成绩进行平均,然后根据要求将它们吐出到列表框中......快乐编码!

P.S。我也没有做任何错误处理,你可能想要添加或者至少实现一些东西来处理这种情况...

Public Class Form1

 Private arrStudents(3) As String 'Student's name array (4)
 Private arrScores(3, 4) As Integer 'Students scores (5 each)

'Start getting the data we need
Private Sub btnGetStudents_Click(sender As Object, e As EventArgs) Handles btnGetStudents.Click
    Dim strStudent As String = String.Empty
    Dim intScore As Integer = 0
    Dim intPlace As Integer = 0

    'Get the students name...
    For i As Integer = 0 To arrStudents.Length - 1
        Do Until strStudent <> String.Empty
            strStudent = InputBox("Please enter student's name: ", "Gather Student Grades")
        Loop
        arrStudents(i) = strStudent

        'Reset our variable...
        strStudent = String.Empty

        'Get the students grades...
        For s As Integer = 0 To arrScores.Length - 1

            Do Until intScore > 0
                intScore = CInt(InputBox("Please enter student's scores: ", "Gather Student Scores"))
            Loop

            If (intPlace = 4 AndAlso i = arrStudents.Length) Then
                intPlace = 0
                arrScores(i, s) = intScore
                intScore = 0
            ElseIf intPlace = 4 Then
                intPlace = 0
                arrScores(i, s) = intScore
                intScore = 0
                Exit For
            Else
                arrScores(i, intPlace) = intScore
                intPlace += 1
            End If

            'Reset our variables...
            intScore = 0
        Next
    Next

    'Calculate and output the data to the listbox...
    GetStudentAverages(arrStudents, arrScores)

End Sub

'Function to average per student grades and then display them all in the listbox...
Private Sub GetStudentAverages(ByVal arStudent As Array, ByVal arScore As Array)
    Dim strStudentData As String = String.Empty
    Dim intAverage As Integer = 0
    Dim intPlace As Integer = 0

    'Start averaging the students scores and then add them to the listbox...
    For i As Integer = 0 To arStudent.Length - 1
        For g As Integer = 0 To arScore.Length - 1
            If intPlace = arStudent.Length Then
                intAverage += arScore(i, intPlace)
                Exit For
            Else
                intAverage += arScore(i, intPlace)
                intPlace += 1
            End If
        Next
        intAverage = CInt(intAverage / 5)
        intPlace = 0

        'Output the student information...
        ListBox1.Items.Add("Student: " & arStudent(i).ToString & " Average: " & intAverage.ToString)
        intAverage = 0
    Next
End Sub

End Class