2

一旦计数器达到 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
4

3 回答 3

1

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

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
于 2013-10-08T19:46:33.183 回答
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
于 2013-10-08T19:46:49.293 回答
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
于 2013-10-08T19:45:16.497 回答