在VB

时间:2016-03-22 13:33:30

标签: vb.net

我想将当前游戏周期的猜测序数增加1.我将该值初始设置为0,但在1之后不会更新。尝试次数相同。我将值设置为21,很快就会更新为20但不会更新。

Option Strict On
Option Explicit On
Public Class Form1
    Private ReadOnly rand As New Random
    Private value As Integer

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.value = rand.Next(minValue:=1, maxValue:=30)  'Setting up random number

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim guess As Integer        'guess number
        Dim numTry As Integer       'No. of trys
        Dim OrdNo As Integer        'Ordinal Number
        Dim Score As Integer        'Score

        Score = 0                   'Initial value of score set as 0
        numTry = 21
        OrdNo = CInt(1)             'Initial value of ordinal set as 1 
        guess = CInt(TextBox1.Text)
        OrdNo = +1
        Label5.Text = CStr(OrdNo)

        'Show Message Box if the guess is not within the range
        If 1 > guess Then
            MessageBox.Show("Input within the range (1-30)", "Error", MessageBoxButtons.OK)
            Exit Sub
        End If

        'Show Message Box if the guess is not within the range
        If guess > 30 Then
            MessageBox.Show("Input within the range (1-30)", "Error", MessageBoxButtons.OK)
            Exit Sub
        End If

        'Display result and message when guess is larger than the lucky number
        If guess > Me.value Then
            Label11.Text = CStr(guess)
            Label10.Text = "The Lucky Number is smaller than your guess"
            OrdNo = OrdNo + 1
            Label5.Text = CStr(OrdNo)
            numTry = numTry - 1
            Label4.Text = CStr(numTry)
        End If

        'Display result and message when guess is smaller than lucky number
        If guess < Me.value Then
            Label11.Text = CStr(guess)
            Label10.Text = "The Lucky Number is larger than your guess"
            OrdNo = OrdNo + 1
            Label5.Text = CStr(OrdNo)
            numTry = numTry - 1
            Label4.Text = CStr(numTry)

        End If

        'Display result and message when guess is equal to the lucky number
        If guess = Me.value Then
            Label11.Text = CStr(guess)
            Label10.Text = "Congratulations ! This is the lucky number"
            Score = +10 'Increase the score by 10
            Label6.Text = CStr(Score)
            numTry = numTry - 1
            OrdNo = 1
            Me.value = rand.Next(minValue:=1, maxValue:=30)

            If numTry = 0 Then Application.Exit()

        End If

    End Sub

End Class

1 个答案:

答案 0 :(得分:2)

考虑每次点击按钮时发生的事情。为了简化,我们只看一个变量:

Dim numTry As Integer
numTry = 21
numTry = numTry - 1

所以每次点击按钮时,都会发生三件事:

  • 声明变量
  • 将其设为21
  • 减1

每一次。

但你每次只想发生的唯一事情就是:

  • 减1

在这种情况下,不应重新声明变量并在每次单击按钮时重新初始化变量。相反,将它们移动到类级别。像这样:

Private numTry As Integer = 21

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    ' other code...
    numTry = numTry - 1
    ' other code...
End Sub

这样,变量本身在对象的生命周期开始时只声明和初始化一次,然后在每次单击按钮时更新。

注意:这会在 ASP.NET 情况下表现不同,因为对象的生命周期是按请求进行的。在这种情况下,而不是存储在一个类级变量,你想要存储在一些其他持久性介质中,例如Session State。)