Vb.net VB中局部变量的更新
我想将当前游戏周期的猜测序号增加1。我最初将该值设置为0,但在1之后不会更新。尝试次数也是如此。我已经将该值设置为21,它很快会更新为20,但之后不会更新Vb.net VB中局部变量的更新,vb.net,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.Obj
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
私有只读rand作为新随机变量
作为整数的私有值
私有子表单1_Load(发送方作为System.Object,e作为System.EventArgs)处理MyBase.Load
Me.value=rand.Next(minValue:=1,maxValue:=30)设置随机数
端接头
私有子按钮1\u单击(发送者作为System.Object,e作为System.EventArgs)处理按钮1。单击
Dim guess作为整数的猜测数
Dim numTry作为整数“尝试次数”
Dim OrdNo作为整数的序数
将分数设置为整数的分数
分数=0'分数的初始值设置为0
numTry=21
OrdNo=CInt(1)'序数集的初始值为1
guess=CInt(TextBox1.Text)
OrdNo=+1
Label5.Text=CStr(OrdNo)
'如果猜测不在范围内,则显示消息框
如果1>猜猜看
MessageBox.Show(“输入范围(1-30)”,“错误”,MessageBox按钮。确定)
出口接头
如果结束
'如果猜测不在范围内,则显示消息框
如果猜测>30,则
MessageBox.Show(“输入范围(1-30)”,“错误”,MessageBox按钮。确定)
出口接头
如果结束
'猜测大于幸运数字时显示结果和消息
如果guess>Me.value,那么
Label11.Text=CStr(猜测)
Label10.Text=“幸运数字小于您的猜测”
OrdNo=OrdNo+1
Label5.Text=CStr(OrdNo)
numTry=numTry-1
Label4.Text=CStr(numTry)
如果结束
'猜测小于幸运数字时显示结果和消息
如果猜
考虑每次单击按钮时发生的情况。为了简化,让我们只看一个变量:
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情况下会有不同的行为,因为对象的寿命是每个请求的。在这种情况下,您不需要存储在类级变量中,而是希望存储在其他持久性介质中,例如会话状态。)考虑每次单击按钮时发生的情况。为了简化,让我们只看一个变量:
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情况下会有不同的行为,因为对象的生命周期是每个请求的生命周期。在这种情况下,您不需要存储在类级变量中,而是希望存储在其他一些持久性介质中,例如会话状态。)函数或子函数中声明的变量是函数或子函数的本地变量,当它结束时,它们就被摧毁了。将这些变量声明向上移动到函数外部,使其成为模块级,那么该变量将在类的生命周期内有效。你的意思是我应该将其设置为全局变量吗?如果我这样做,我就无法设置该变量的初始值???函数或子函数中声明的变量是函数或子函数的局部变量,并且在结束时会被销毁。将这些变量声明向上移动到函数外部,使其成为模块级,那么该变量将在类的生命周期内有效。你的意思是我应该将其设置为全局变量吗?如果我这样做,我就无法设置该变量的初始值???