Vb6 风险游戏不显示分数
我正在为我的班级做一个题为“风险!”的作业,它的基础是你从1000分开始,输入一个风险数字。你掷2个骰子。如果它是偶数,你输了,输入从分数中删除。如果是奇数,您将获胜,并将输入添加到分数中。由于某些原因,分数显示不正确Vb6 风险游戏不显示分数,vb6,Vb6,我正在为我的班级做一个题为“风险!”的作业,它的基础是你从1000分开始,输入一个风险数字。你掷2个骰子。如果它是偶数,你输了,输入从分数中删除。如果是奇数,您将获胜,并将输入添加到分数中。由于某些原因,分数显示不正确 Private Sub cmdQuit_Click() Unload Me End Sub Private Sub cmdRollDice_Click() intNumOutput1 = Int(Rnd * 6) + 1 intNumOutput2 =
Private Sub cmdQuit_Click()
Unload Me
End Sub
Private Sub cmdRollDice_Click()
intNumOutput1 = Int(Rnd * 6) + 1
intNumOutput2 = Int(Rnd * 6) + 1
lblNumOutput1.Caption = intNumOutput1
lblNumOutput2.Caption = intNumOutput2
intBothOutputs = intNumOutput1 + intNumOutput2
If intBothOutputs Mod 2 > 0 Then
intScore = intScore + intNumInput
MsgBox "odd, win"
Else
intScore = intScore - intNumInput
MsgBox "even, lose"
End If
lblTotal.Caption = "Your new point total is " & intScore
End Sub
Private Sub Form_Load()
Randomize
Dim intScore As Integer
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
Dim intNumInput As Integer
txtNumInput.Text = intNumInput
intScore = 1000
txtNumInput.Text = ""
lblNumOutput1.Caption = ""
lblNumOutput2.Caption = ""
End Sub
当您想在多个方法(例如sub、function)中使用变量时,可以在任何方法之外声明变量。 现在,由于您在
Form\u Load
中声明了变量,因此不能在cmdRollDice\u Click
或任何其他方法中使用它们。那么,当您在声明它们的方法之外的方法中使用它们时会发生什么呢?好的,如果在代码顶部有Option Explicit
语句,您将得到一个运行时错误。如果不这样做(这是您当前的情况),则每次调用该方法时,变量都将被初始化为零值(注意:它们现在不是表单\u Load
中声明的相同变量)
因此,您需要在文件顶部(在所有函数/子函数之前)声明变量,如下所示:
Dim intScore As Integer
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
Dim intNumInput As Integer
' The rest of your code
Private Sub Form_Load()
End Sub
Private Sub cmdRollDice_Click()
End Sub
'
'
因此,作为一条规则:只有在不需要在方法外部使用变量时,才在方法内部声明变量
有关这方面的更多信息,请阅读
希望有帮助:)当您想在多个方法(例如sub、function)中使用变量时,您可以在任何方法之外声明变量。 现在,由于您在
Form\u Load
中声明了变量,因此不能在cmdRollDice\u Click
或任何其他方法中使用它们。那么,当您在声明它们的方法之外的方法中使用它们时会发生什么呢?好的,如果在代码顶部有Option Explicit
语句,您将得到一个运行时错误。如果不这样做(这是您当前的情况),则每次调用该方法时,变量都将被初始化为零值(注意:它们现在不是表单\u Load
中声明的相同变量)
因此,您需要在文件顶部(在所有函数/子函数之前)声明变量,如下所示:
Dim intScore As Integer
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
Dim intNumInput As Integer
' The rest of your code
Private Sub Form_Load()
End Sub
Private Sub cmdRollDice_Click()
End Sub
'
'
因此,作为一条规则:只有在不需要在方法外部使用变量时,才在方法内部声明变量
有关这方面的更多信息,请阅读
希望有帮助:)
Option Explicit
Private intScore As Integer
Private intNumOutput1 As Integer
Private intNumOutput2 As Integer
Private intBothOutputs As Integer
Private intNumInput As Integer
Private Sub cmdRollDice_Click()
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
intNumOutput1 = Int(Rnd * 6) + 1
intNumOutput2 = Int(Rnd * 6) + 1
lblNumOutput1.Caption = intNumOutput1
lblNumOutput2.Caption = intNumOutput2
intBothOutputs = intNumOutput1 + intNumOutput2
If intBothOutputs Mod 2 > 0 Then
intScore = intScore + intNumInput
MsgBox "odd, win"
Else
intScore = intScore - intNumInput
MsgBox "even, lose"
End If
lblTotal.Caption = "Your new point total is " & CStr(intScore)
End Sub
Private Sub txtNumInput_Change()
If IsNumeric(txtNumInput.Text) Then
intNumInput = CInt(txtNumInput.Text)
End If
End Sub
Option Explicit
Private intScore As Integer
Private intNumOutput1 As Integer
Private intNumOutput2 As Integer
Private intBothOutputs As Integer
Private intNumInput As Integer
Private Sub cmdRollDice_Click()
Dim intNumOutput1 As Integer
Dim intNumOutput2 As Integer
Dim intBothOutputs As Integer
intNumOutput1 = Int(Rnd * 6) + 1
intNumOutput2 = Int(Rnd * 6) + 1
lblNumOutput1.Caption = intNumOutput1
lblNumOutput2.Caption = intNumOutput2
intBothOutputs = intNumOutput1 + intNumOutput2
If intBothOutputs Mod 2 > 0 Then
intScore = intScore + intNumInput
MsgBox "odd, win"
Else
intScore = intScore - intNumInput
MsgBox "even, lose"
End If
lblTotal.Caption = "Your new point total is " & CStr(intScore)
End Sub
Private Sub txtNumInput_Change()
If IsNumeric(txtNumInput.Text) Then
intNumInput = CInt(txtNumInput.Text)
End If
End Sub
更详细地描述这个问题。“分数显示不正确”是什么意思?分数最初显示为“0”而不是1000,然后加上或减去输入,以便更详细地描述问题。“分数显示不正确”是什么意思?分数最初显示为“0”而不是1000,然后加上或减去您需要的输入