在Visual Basic中创建GPA计算器的问题

时间:2012-08-24 17:56:13

标签: vb.net calculator

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

我只是视觉基础的初学者,但想知道我在计算GPA时出错的地方,即使从Select中积累了一些东西...... Case也是可能的。如果没有,那么我必须以不同的方式输入,然后如上所示。如果有人能给我提示我做错了什么,我将不胜感激。

3 个答案:

答案 0 :(得分:1)

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

我们都知道,对于成绩,A&gt; 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

答案 1 :(得分: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

答案 2 :(得分:0)

比较你不能使用的字符串&gt; =你说的是等级是否大于或等于一个字符串,它只能相等或不相等。

&lt; = pr&gt; =仅适用于比较数字。