我试图写一个简单的GPA计算器,用户从组合框(A,B,C,D,F)上的项目和课程的学分时间输入成绩,计算将输出到文本框中。但是,我一直在文本框中获得NaN结果,但我无法理解为什么。我只有一点编程经验,所以我很感激任何帮助!
Public Class Form1
Public points As Double, hours As Integer
Private Sub btnRecord_Click(sender As Object, e As EventArgs) Handles btnRecord.Click
Dim gpaPoints As Double
Dim creditHours As Integer
Dim grade As String
If cmbGrades.SelectedIndex < 0 Then
MsgBox("Please select a grade.")
Exit Sub
ElseIf txtHours.Text = "" Then
MsgBox("Please enter credit hours.")
Else
grade = cmbGrades.SelectedItem
creditHours = CInt(txtHours.Text)
gpaPoints = CalcGPA(grade, creditHours)
points += gpaPoints
hours += creditHours
ClearList()
End If
End Sub
Public Sub ClearList()
cmbGrades.SelectedIndex = -1
cmbGrades.Text = "Select a grade"
txtHours.Text = ""
End Sub
Public Function CalcGPA(grade As String, creditHours As Integer)
Dim gpaPoints As Double
Select Case (grade)
Case "A"
gpaPoints = 4
Case "B"
gpaPoints = 3
Case "C"
gpaPoints = 2
Case "D"
gpaPoints = 1
Case Else
gpaPoints = 0
End Select
Return gpaPoints
End Function
Private Sub btnGpa_Click(sender As Object, e As EventArgs) Handles btnGpa.Click
Dim calcGpa As Double = points / hours
txtGpa.Text = FormatNumber(calcGpa, 2)
End Sub
End Class
我基本上有一个记录按钮可以正常清除列表并允许某人输入二年级,但计算按钮不起作用,我无法解决这个问题。我衷心感谢任何帮助。
答案 0 :(得分:1)
Private Sub btnGpa_Click(sender As Object, e As EventArgs) Handles btnGpa.Click
'Dim calcGpa As Double = points / hours '(**did you assign values here?** if not remove this)
dim answer as double = 0
CalcGPA(cmbGrades.SelectedValue, txtHours.Text )
txtGpa.Text = FormatNumber(answer, 2)
End Sub
答案 1 :(得分:1)
作为一般经验法则,如果您正在查看NaN,您可以查看以下内容: