1

我的累加器 (decTotalCredits) 工作不正常,每次输入时都重置为零。我需要累加器添加每个用户输入,直到达到 125。通常情况下,我确信它很小。提前感谢您的所有帮助!

    ' Declaring variable for Credit Entered by User
    Dim decCredit As Decimal = CDec(txtCredit.Text)
    ' Declaring the accumulator
    Dim decTotalCredits As Decimal

    If IsNumeric(txtCredit.Text) Then
        decCredit = Convert.ToDecimal(txtCredit.Text)
        Select Case decCredit
            Case Is = 5
            Case Is = 10
            Case Is = 25
            Case Is = 100
            Case Else
                MsgBox("Please enter a valid coin amount", , "Invalid Amount Entered")
        End Select

        If decTotalCredits < 125 Then

            decTotalCredits += decCredit
            lblTotal.Text = CStr(decTotalCredits)
            lblTotal.Visible = True

            txtCredit.Clear()
            txtCredit.Focus()

        Else

            ' Once the credits are reached the prompt to make selection is visible. 
            lblMakeSelection.Visible = True

            ' Once the credits are reached, the buttons for selection become enabled.
            btnDietPepsi.Enabled = True
            btnPepsi.Enabled = True
            btnSierraMist.Enabled = True
            btnLemonade.Enabled = True
            btnDrPepper.Enabled = True
            btnWater.Enabled = True
        End If
    Else
        MsgBox("Please enter a valid Coin amount", , "Input Error")


    End If
4

1 回答 1

3

正在重新声明..

  • 如果这是在循环中,则需要在循环之外声明 DecTotalCredits。
  • 如果这是在事件处理程序中,则需要在代码顶部(在类声明的正下方)声明 DecTotalCredits 这样它就不会在每次处理程序运行时重新声明
于 2012-04-11T17:14:30.493 回答