0

我编写了一个 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

感谢您提供的任何帮助!

4

1 回答 1

0

看看Int32.TryParse方法。它将尝试解析数据,如果成功则返回一个值。

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

于 2013-03-27T19:11:00.857 回答