variables - 我试图在计数器达到 10 时重置计数器,但似乎无法正确执行。

标签 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

最佳答案

输出文本后,您应该能够在 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

关于variables - 我试图在计数器达到 10 时重置计数器,但似乎无法正确执行。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19256816/

相关文章:

php - 如何从 HTML/PHP 中的单个 <select> 变量获取多个值?

javascript - 使用 jQuery 获取 Google+ 订阅计数

带有计数器的 AngularJS 文本输入验证

laravel - Laravel Blade 循环中 undefined variable $loop

javascript - 在 Jquery 中仅突出显示一行

ruby - 解释 ruby​​ 中具有相同名称和其他属性的方法和变量的行为

c# - 通过将方法作为变量发送来简化代码

python - 在文本框 python 中以特定格式插入列表中的值

python - 从 OrderedDict 获取键计数,其中键是一个元组

javascript显示文本然后在一段时间后显示新文本