我试图重置计数器一旦计数器击中10,我似乎无法正确。

时间:2013-10-08 19:40:40

标签: variables counter

一旦计数器达到10,它就会显示“你的门票是免费的”。我无法弄清楚如何重置计数器,以便再次点击10次将显示“您的门票是免费的”

Public Class Form1
Dim intCounter As Integer

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim intTicketPrice As Integer = Val(Me.txtTicketNum.Text) * 8
    intCounter = intCounter + 1

    If intCounter = 10 Then
        Me.lblFeed.Text = "Your tickets are free!!!"

    ElseIf intTicketPrice Then
        Me.lblFeed.Text = "Your tickets cost: " & intTicketPrice & " Dollars"
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    intCounter = 0
End Sub

End Class

3 个答案:

答案 0 :(得分:1)

输出文本后,您应该只能在if语句中添加intCounter = 0。所以:

If intCounter = 10 Then
    Me.lblFeed.Text = "Your tickets are free!!!"
    intCounter = 0
ElseIf intTicketPrice Then
    Me.lblFeed.Text = "Your tickets cost: " & intTicketPrice & " Dollars"
End If

或者,您可以在不重置计数器的情况下使用,而是在计数器上执行模数运算 -

If (intCounter % 10) = 0 Then
    Me.lblFeed.Text = "Your tickets are free!!!"
ElseIf intTicketPrice Then
    Me.lblFeed.Text = "Your tickets cost: " & intTicketPrice & " Dollars"
End If

答案 1 :(得分:1)

今天可能是你对.Net的期望过多:)你没有在你的代码中将计数器设置为0

Public Class Form1
        Dim intCounter As Integer

        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
            Dim intTicketPrice As Integer = Val(Me.txtTicketNum.Text) * 8
            intCounter = intCounter + 1

            If intCounter = 10 Then
                Me.lblFeed.Text = "Your tickets are free!!!"
                intCounter = 0
            ElseIf intTicketPrice Then
                Me.lblFeed.Text = "Your tickets cost: " & intTicketPrice & " Dollars"
            End If
        End Sub

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            intCounter = 0
        End Sub

    End Class

答案 2 :(得分:0)

这看起来像Vb表单应用。我想你只需要在你的条件中重置你的intCounter:

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim intTicketPrice As Integer = Val(Me.txtTicketNum.Text) * 8
    intCounter = intCounter + 1

    If intCounter = 10 Then
        Me.lblFeed.Text = "Your tickets are free!!!"
        intCounter = 0
    ElseIf intTicketPrice Then
        Me.lblFeed.Text = "Your tickets cost: " & intTicketPrice & " Dollars"
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    intCounter = 0
End Sub