在数组中搜索正确的字母等级并使用循环显示它

时间:2014-04-08 19:57:21

标签: vb.net

我的程序首先让用户输入可能的点,然后按btnCreate后,它会在lstBox中显示评分等级。之后它将焦点传递给txtEarned,用户输入获得的点数,然后按下btnDisplay,btnDisplay将根据创建的评分等级显示相应的字母等级。 btnDisplay根本没有显示正确的字母等级。它显示" F"但是如果我继续按btnDisplay它会改为D,C,B,然后是A。

Option Explicit On
Option Strict On
Option Infer Off

公共类frmMain

Dim dblPossible As Double
Dim dblEarned As Double
'Dim dblPercentage As Double
Dim dblMinimumPoints() As Double = {0.0, 0.6, 0.7, 0.8, 0.9}
Dim strGrade() As String = {"F", "D", "C", "B", "A"}
Dim gradeIndex As Integer = 0


Private Sub txtPossible_TextChanged(sender As Object, e As EventArgs) Handles txtPossible.TextChanged
    ' disable the Display Grade button until the user creates a new grading scale, then
    ' clear the grade

    btnDisplay.Enabled = False
    lblGrade.Text = String.Empty
End Sub

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    ' displays a student's grade
    For Each minimum As Double In dblMinimumPoints
        If dblEarned >= minimum Then
            lblGrade.Text = strGrade(gradeIndex)
            gradeIndex += 1
        End If
    Next

    txtEarned.ReadOnly = False


End Sub

Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    ' fill arrays with values

    Double.TryParse(txtPossible.Text, dblPossible)
    Double.TryParse(txtEarned.Text, dblEarned)

    For counter As Integer = 0 To 4 Step 1
        dblMinimumPoints(counter) *= dblPossible
        lstBox.Items.Add(dblMinimumPoints(counter).ToString)
    Next

    ' enable the Display Grade button 
    btnDisplay.Enabled = True
    txtEarned.ReadOnly = False

    txtEarned.Focus()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub txtPossible_Enter(sender As Object, e As EventArgs) Handles txtPossible.Enter
    txtPossible.SelectAll()
End Sub


Private Sub CancelKeys(sender As Object, e As KeyPressEventArgs) Handles txtPossible.KeyPress
    ' accepts only numbers and the Backspace key

    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
        e.Handled = True
    End If
End Sub

Private Sub txtEarned_TextChanged(sender As Object, e As EventArgs)
    lblGrade.Text = String.Empty
End Sub

结束班

2 个答案:

答案 0 :(得分:1)

我没有看到dblEarned被设置在任何地方,这意味着它将始终是F.每次单击按钮时等级更改的原因是因为gradeIndex每次都会递增而不会重置。

要解决此问题,请在循环之前设置dblEarned并将gradeIndex重置为0。 请参阅下面的快速修订

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
gradeIndex = 0
dblEarned = SETVALUE
For Each minimum As Double In dblMinimumPoints
    If dblEarned >= minimum Then
        lblGrade.Text = strGrade(gradeIndex)
        gradeIndex += 1
    End If
Next

txtEarned.ReadOnly = False 
End Sub

答案 1 :(得分:0)

我修好了!我刚刚将double.TryParse(txtEarned.Text,dblEarned)从btnCreate_Click移动到btnDisplay_Click。我是一个范围问题。