我的目标是这样做:将文件中的数据读入数组,加载列表框,计算每个学生的平均值并确定字母等级,并计算班级平均值:
我声明的变量(全局):
Dim project,midterm,hw1,hw2,final,count,average As Integer
Dim letterGrade, lastName, firstName As String
Dim classAverage As Double
我的代码:
Dim array1 As String()= New String(6){}
Dim streamreader1 As StreamReader = File.OpenText("grades.csv")
count = 0
classAverage = 0
Do While (streamreader1.Peek() <> -1)
array1 = Strings.Split(streamreader1.ReadLine(), ",", -1)
firstName(count) = array1(0)
lastName(count) = array1(1)
lstNames.Items.Add(lastName(count) & ", " & firstName(count))
hw1(count) = Convert.ToString(array1(2))
hw2(count) = Convert.ToString(array1(3))
project(count) = Convert.ToString(array1(4))
midterm(count) = Convert.ToString(array1(5))
final(count) = Convert.ToString(array1(6))
average(count) = Math.Round(Math.Truncate((((((hw1(count) + hw2(count)) + (2 * project(count))) + (4 * (midterm(count) + final(count)))) / 12) + 0.5)))
classAverage = (classAverage + ((((hw1(count) + hw2(count)) + (2 * project(count))) + (4 * (midterm(count) + final(count)))) / 12))
Dim num1 As Integer = average(count)
If (num1 >= 93) Then
letterGrade(count) = "A"
ElseIf (num1 >= 90) Then
letterGrade(count) = "A-"
ElseIf (num1 >= 87) Then
letterGrade(count) = "B+"
ElseIf (num1 >= 83) Then
letterGrade(count) = "B"
ElseIf (num1 >= 80) Then
letterGrade(count) = "B-"
ElseIf (num1 >= 77) Then
letterGrade(count) = "C+"
ElseIf (num1 >= 73) Then
letterGrade(count) = "C"
ElseIf (num1 >= 70) Then
letterGrade(count) = "C-"
ElseIf (num1 >= 67) Then
letterGrade(count) = "D+"
ElseIf (num1 >= 63) Then
letterGrade(count) = "D"
ElseIf (num1 >= 60) Then
letterGrade(count) = "D"
Else
letterGrade(count) = "E"
End If
count = (count + 1)
Loop
classAverage = (classAverage / count)
lstNames.SelectedIndex = 0
PopulateLabels(0)
streamreader1.Close()
答案 0 :(得分:5)
看起来letterGrade
是String
,而不是字符串数组。您需要将其正确地声明为数组。
编辑后......
我建议创建一个Student
类,其中包含文本文件中的值。它还包含针对对象本身执行的CalculateAverage()
和CalculateLetterGrade()
的方法。然后将文本文件中的每一行转储到Student
对象中,并添加到List(Of Student)
。获得此列表后,您可以遍历一个很好的简单对象列表来计算类平均值。
这样的事情:
Public Class Student
Public FirstName As String
Public LastName As String
Public Project As Integer
...
Public Final As Integer
Public Function CalculateAverage() As Integer
... ' Your calculation function
End Function
Public Function CalculateLetterGrade() As String
... ' Your calculation function
End Function
End Class
Dim students As New List(Of Student)
'Inside your loop:
Dim student As New Student()
student.Project = Convert.ToString(array1(4))
... ' etc.
students.Add(student)
此时你将拥有一个简单的学生集合,你可以循环学习,计算你的班级平均水平或其他你需要的东西。