ノックアウトしたばかりなので、おそらく片付けられる可能性があります。
テストするには、複数行の TEXTBOX (textbox1 という名前) と Button (button1 という名前) をフォームにドロップし、ボタンをクリックします。
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'-> Data
' Alice, 76, 45, 87, 23
' Ben, 76, 48, 85, 65
' Julie, 76, 36, 49, 86
' Monica, 85, 90, 83, 76
'-> Variables
Dim strTextLine As String
Dim intCounter As Integer
Dim chrCurrentCharacter As String
Dim strBuffer As String
Dim strResults As String
Dim booNameFound As Boolean
'-> Processing
'1 calculate the average score and grade received by each student
Dim intIndividualAverage As Integer = 0
'2 Display who received the highest mark
Dim intHighest As Integer = 0
Dim intTmpHighest As Integer = 0
Dim strHighest As String = ""
'3 Display the average mark of all students in the class
Dim intGroupAverage As Integer = 0
'4 List students who received grade "C"
Dim strGradeCStudents = ""
Dim strName As String = ""
'-> Grading Rules
' A (70-100), B(60-69),C(50-59),D(40-49),F(0-39)
'-> Open file and process data one line at a time
Try
' Create an instance of StreamReader to read from a file
Dim objStreamReader As StreamReader = New StreamReader("c:\temp\test.txt")
' Read the lines from the file until the end of the file is reached.
Do
strTextLine = objStreamReader.ReadLine()
'if not eof or empty text line
If Trim(strTextLine) <> "" Then
'reset vars
strName = ""
strBuffer = ""
strResults = ""
booNameFound = False
intIndividualAverage = 0
For intCounter = 1 To Len(strTextLine)
chrCurrentCharacter = Mid(strTextLine, intCounter, 1)
'is this a comma?
If chrCurrentCharacter = "," Then
'has the name been found?
If booNameFound Then
'this is a grade result(note there is no Grade E!)
Select Case Val(strBuffer)
Case 70 To 100
strResults = strResults & " A"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
intGroupAverage = intGroupAverage + Val(strBuffer)
Case 60 To 69
strResults = strResults & " B"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
intGroupAverage = intGroupAverage + Val(strBuffer)
Case 50 To 59
strResults = strResults & " C"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
intGroupAverage = intGroupAverage + Val(strBuffer)
'grade C Students
If InStr(strGradeCStudents, strName) > 0 Then
'alreadylisted inthe grade c list
Else
strGradeCStudents = strGradeCStudents & strName & " "
End If
Case 40 To 49
strResults = strResults & " D"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
intGroupAverage = intGroupAverage + Val(strBuffer)
Case Else
strResults = strResults & " F"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
intGroupAverage = intGroupAverage + Val(strBuffer)
End Select
strBuffer = ""
Else
'this is name
booNameFound = True
strResults = strBuffer
strName = strBuffer
strBuffer = ""
End If
'if its not a space
ElseIf chrCurrentCharacter <> " " Then
strBuffer = strBuffer & chrCurrentCharacter
Else
'Spaces are not processed
End If
Next
'Process LAST result because there is no comma after it
'so it must be done after the endof the line
Select Case Val(strBuffer)
Case 70 To 100
strResults = strResults & " A"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
Case 60 To 69
strResults = strResults & " B"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
Case 50 To 59
strResults = strResults & " C"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
Case 40 To 49
strResults = strResults & " D"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
Case Else
strResults = strResults & " F"
intIndividualAverage = intIndividualAverage + Val(strBuffer)
End Select
strBuffer = ""
'Student average
strResults = strResults & " (avg=" & (intIndividualAverage / 4) & ")" & vbCrLf & vbCrLf
TextBox1.Text = TextBox1.Text & strResults
'Console.WriteLine(strResults)
'Highest?
If intHighest = 0 Then
strHighest = strName
ElseIf intTmpHighest > intHighest Then
strHighest = strName
ElseIf intTmpHighest = intHighest Then
strHighest = strHighest & " " & strName
End If
Else
'The line was empty
End If
Loop Until strTextLine Is Nothing
objStreamReader.Close()
'-> Display Summary
TextBox1.Text = TextBox1.Text & vbCrLf & "SUMMARY" & vbCrLf
TextBox1.Text = TextBox1.Text & "Highest Marks: " & strHighest & vbCrLf
TextBox1.Text = TextBox1.Text & "Group Average: " & ((intGroupAverage / 4) / 4) & vbCrLf
TextBox1.Text = TextBox1.Text & "Grade C Students: " & strGradeCStudents & vbCrLf
Catch Ex As Exception
' Let the user know what went wrong.
TextBox1.Text = TextBox1.Text & "The file could not be read:"
TextBox1.Text = TextBox1.Text & Ex.Message
'Console.WriteLine("The file could not be read:")
'Console.WriteLine(Ex.Message)
End Try
End Sub