GPA计算器文本框问题

时间:2013-03-27 19:08:01

标签: vb.net

我编写了一个GPA计算器,用于输入课程名称的8个文本框,每个课程的小时数8个文本框,以及最后8个用于这些课程中的字母等级的文本框。我在使用信用时数的8个文本框时遇到了问题。如果我不觉得每个人都有一个数字,它会给我错误“输入字符串的格式不正确”。我想把它编程到如果用户只拿5个课程,他们可以将其他3个文本框留空,并且“0”将被放入他们没有填写学分时间的文本框中,只需计算GPA输入了5个班级。如果用户输入所有8个文本框中的信用时间,它可以正常工作但是,如果他们没有填写所有8个,我上面提到的错误代码会出现....

这是我到目前为止的代码。

Public Class Form1

Dim QualityPoints As Double
Dim CourseName, hours As String

Private Sub btnTotalHours_Click(sender As Object, e As EventArgs) Handles btnTotalHours.Click
    Dim totalHours As String
    Dim Credit1, Credit2, Credit3, Credit4, Credit5, Credit6, Credit7, Credit8 As Integer
    Credit1 = Integer.Parse(txtBoxCredit1.Text)
    Credit2 = Integer.Parse(txtBoxCredit2.Text)
    Credit3 = Integer.Parse(txtBoxCredit3.Text)
    Credit4 = Integer.Parse(txtBoxCredit4.Text)
    Credit5 = Integer.Parse(txtBoxCredit5.Text)
    Credit6 = Integer.Parse(txtBoxCredit6.Text)
    Credit7 = Integer.Parse(txtBoxCredit7.Text)
    Credit8 = Integer.Parse(txtBoxCredit8.Text)

    totalHours = CStr(txtBoxTotalHours.Text)
    txtBoxTotalHours.Text = (Credit1 + Credit2 + Credit3 + Credit4 + Credit5 + Credit6 + Credit7 + Credit8)

End Sub

Private Sub btnTotalQualityPoints_Click(sender As Object, e As EventArgs) Handles btnTotalQualityPoints.Click

    Select Case txtBoxGrade1.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit1.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit1.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit1.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit1.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit1.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit1.Text)

    End Select

    Select Case txtBoxGrade2.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit2.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit2.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit2.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit2.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit2.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit2.Text)

    End Select

    Select Case txtBoxGrade3.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit3.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit3.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit3.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit3.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit3.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit3.Text)

    End Select

    Select Case txtBoxGrade4.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit4.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit4.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit4.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit4.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit4.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit4.Text)

    End Select

    Select Case txtBoxGrade5.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit5.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit5.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit5.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit5.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit5.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit5.Text)

    End Select

    Select Case txtBoxGrade6.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit6.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit6.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit6.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit6.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit6.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit6.Text)

    End Select

    Select Case txtBoxGrade7.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit7.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit7.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit7.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit7.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit7.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit7.Text)

    End Select

    Select Case txtBoxGrade8.Text
        Case "A"
            QualityPoints += (4 * txtBoxCredit8.Text)
        Case "B"
            QualityPoints += (3 * txtBoxCredit8.Text)
        Case "C"
            QualityPoints += (2 * txtBoxCredit8.Text)
        Case "D"
            QualityPoints += (1 * txtBoxCredit8.Text)
        Case "F"
            QualityPoints += (0 * txtBoxCredit8.Text)
        Case " "
            QualityPoints += (0 * txtBoxCredit8.Text)

    End Select

    txtBoxTotalQualityPoints.Text = QualityPoints

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    txtBoxCourse1.Clear()
    txtBoxCourse2.Clear()
    txtBoxCourse3.Clear()
    txtBoxCourse4.Clear()
    txtBoxCourse5.Clear()
    txtBoxCourse6.Clear()
    txtBoxCourse7.Clear()
    txtBoxCourse8.Clear()

    txtBoxCredit1.Clear()
    txtBoxCredit2.Clear()
    txtBoxCredit3.Clear()
    txtBoxCredit4.Clear()
    txtBoxCredit5.Clear()
    txtBoxCredit6.Clear()
    txtBoxCredit7.Clear()
    txtBoxCredit8.Clear()

    txtBoxGrade1.Clear()
    txtBoxGrade2.Clear()
    txtBoxGrade3.Clear()
    txtBoxGrade4.Clear()
    txtBoxGrade5.Clear()
    txtBoxGrade6.Clear()
    txtBoxGrade7.Clear()
    txtBoxGrade8.Clear()

    txtBoxTotalGPA.Clear()
    txtBoxTotalHours.Clear()
    txtBoxTotalQualityPoints.Clear()

End Sub

感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:0)

查看Int32.TryParse方法。它将尝试解析数据,并在成功时返回值。

http://msdn.microsoft.com/en-us/library/f02979c7.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1