0
Public Class GPA_Form

Private Sub exitButton_Click(sender As System.Object, e As System.EventArgs) Handles exitButton.Click
    Me.Close()
End Sub

Private Sub entdatButton_Click(sender As System.Object, e As System.EventArgs) Handles entdatButton.Click
    Const Prompt As String = "Enter number of Credit Hours:"
    Const Title As String = "Credit Hours"
    Const Prompt2 As String = "Enter grades:"
    Const Title2 As String = "Grades"
    Dim inputCredit As String
    Dim inputGrades As String
    Dim creditHrs As Integer
    Dim grades As Char
    Dim gradesCounter As Integer
    Dim creditHrsAccumulator As Integer
    Dim point As Integer
    Dim gpaTot As Integer
    Dim pntAccumulator As Integer

    inputCredit = InputBox(Prompt, Title)
    inputGrades = InputBox(Prompt2, Title2)

    Do While inputCredit <> String.Empty
        Integer.TryParse(inputCredit, creditHrs)
        Char.TryParse(inputGrades, grades)

        gradesCounter += 1
        creditHrsAccumulator += creditHrs

        Select Case grades
            Case Is >= "A"
                point = 4
            Case Is >= "B"
                point = 3
            Case Is >= "C"
                point = 2
            Case Is >= "D"
                point = 1
            Case Is >= "F"
                point = 0
        End Select

        pntAccumulator += point

        gpaTot = pntAccumulator / gradesCounter

        tchData.Text = creditHrsAccumulator.ToString("N0")
        numGrEnt.Text = gradesCounter.ToString("N0")
        gpaData.Text = gpaTot.ToString("N2")

        inputCredit = InputBox(Prompt, Title)
        inputGrades = InputBox(Prompt2, Title2)

    Loop


End Sub
End Class

我只是 Visual Basic 的初学者,但想知道我在计算 GPA 时哪里出错了,即使从 Select...Case 中积累一些东西也是可能的。如果不是,那么我将不得不以不同于上面显示的方式输入,当然。如果有人能给我提示我做错了什么,将不胜感激。

4

3 回答 3

1

GPA公式不是gpaTot = pntAccumulator / gradesCounter

gpaTot = 0
creditHrsAccumulator = 0

Do While inputCredit <> String.Empty
    Integer.TryParse(inputCredit, creditHrs)
    Char.TryParse(inputGrades, grades)

    Select Case grades
        Case "A"
            point = 4
        Case "B"
            point = 3
        Case "C"
            point = 2
        Case "D"
            point = 1
        Case "F"
            point = 0
    End Select

    gpaTot= (creditHrsAccumulator*gpaTot + point*creditHrs)/(creditHrsAccumulator + creditHrs)
    gradesCounter += 1
    creditHrsAccumulator += creditHrs

    ...

你可以摆脱pntAccumulator.

于 2012-08-27T14:07:27.670 回答
1

Select ... Case的格式与您使用的不同。

我们都知道对于成绩,A > B > C,但 Visual Basic 不知道。在 Visual Basic 中,这些只是字符,因此它们不具有可比性。Case语句不支持比较运算符,例如,>=它们是隐式的==,并且您不写“Case Is”,只写“Case”。尝试这个:

Select Case grades
    Case "A"
        point = 4
    Case "B"
        point = 3
    Case "C"
        point = 2
    Case "D"
        point = 1
    Case "F"
        point = 0
End Select
于 2012-08-27T13:29:00.343 回答
0

当比较一个字符串时,你不能使用 >= 你说如果等级大于或等于一个字符串,它只能相等或不相等。

<= pr >= 仅适用于比较数字。

于 2014-11-24T20:51:04.987 回答