我的目标是这样做:将文件中的数据读取到数组中,加载列表框,计算每个学生的平均成绩并确定字母成绩,并计算班级平均成绩:
我声明的变量(全局):
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()