大家好,我这里有一些代码,可以从文件中引入一行文本并显示到标签中。我想将标签中的八个值相加并除以 8 以获得平均值,并将该平均分数转换为将显示在新标签中的字母等级。我需要帮助的代码在 btnCal 程序中。
这是我的代码:
Imports System.IO
Public Class Form1
Dim grade As String
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
OFDG1.Filter = "Text Files|*.txt|All Files|*.*"
Dim myResult As DialogResult
myResult = OFDG1.ShowDialog
If myResult = Windows.Forms.DialogResult.OK Then
Dim strReader As StreamReader = File.OpenText(OFDG1.FileName)
Dim aline As String
Do Until strReader.EndOfStream
aline = strReader.ReadLine
Dim myStuff() = aline.Split(","c)
lblStudent.Text = myStuff(0)
lblClassField.Text = myStuff(1)
lblSemesterInput.Text = myStuff(2)
picStudent.Image = Image.FromFile(myStuff(3))
lblInput1.Text = myStuff(4)
lblInput2.Text = myStuff(5)
lblInput3.Text = myStuff(6)
lblInput4.Text = myStuff(7)
lblInput5.Text = myStuff(8)
lblInput6.Text = myStuff(9)
lblMidtermInput.Text = myStuff(10)
lblFinalInput.Text = myStuff(11)
Loop
strReader.Close()
Else
MessageBox.Show("You clicked other than OK")
End If
End Sub
Private Sub btnCal_Click(sender As Object, e As EventArgs) Handles btnCal.Click
grade = CStr(CInt(lblInput1.Text + lblInput2.Text + lblInput3.Text _
+ lblInput4.Text + lblInput5.Text + _
lblInput6.Text + lblMidtermInput.Text + lblFinalInput.Text / 8)
lblFinalLetterGrade.Text = Calculation(CInt(grade))
End Sub
Public Function Calculation(ByVal grade As Integer) As String
Select Case grade
Case Is > 89
Return "A"
Case Is > 79
Return "B"
Case Is > 69
Return "C"
Case Is > 59
Return "D"
Case Else
Return "F"
End Select
End Function
End Class