0

コース名を入力するための 8 つのテキスト ボックス、各コースの時間数を入力するための 8 つのテキスト ボックス、およびそれらのコースで作成した文字の成績を入力するための 8 つのテキスト ボックスがある GPA 計算機をプログラムしました。クレジット時間がかかる 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 に答える