当计数器达到 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/