Я хочу увеличить порядковый номер угадывания для текущего игрового цикла на 1. Я установил значение изначально в 0, но оно не обновляется после 1. То же самое касается количества попыток. Я установил значение в 21, и он скоро обновляется до 20, но не после этого.Обновление локальных переменных в VB
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
переменные, объявленные в функции или sub, являются локальными для функции или sub и уничтожаются, когда они закончены. переместите эти объявления переменных за пределы функции, чтобы сделать ее модульной, и переменная будет жить для жизни класса. – Jeremy
Действительно ли я должен сделать глобальную переменную? Если я сделаю это, я не могу установить начальное значение этой переменной ??? –