Vb.net 蛇和梯子
我必须为一个项目创建一个蛇和梯子游戏。我的棋盘上有30个空格。如果标记超过30,应用程序将崩溃: appCounter.exe中发生“System.NullReferenceException”类型的未处理异常 其他信息:对象引用未设置为对象的实例。“ 如果滚动的数字超过30,我希望标记保持在原来的位置 这是我的密码:Vb.net 蛇和梯子,vb.net,Vb.net,我必须为一个项目创建一个蛇和梯子游戏。我的棋盘上有30个空格。如果标记超过30,应用程序将崩溃: appCounter.exe中发生“System.NullReferenceException”类型的未处理异常 其他信息:对象引用未设置为对象的实例。“ 如果滚动的数字超过30,我希望标记保持在原来的位置 这是我的密码: Public Class frmBoard Dim intScore As Integer Private Sub btnOK_Click(sender As Object,
Public Class frmBoard
Dim intScore As Integer
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
'declartions
Dim intValue As Integer
Dim strCounterName As String
Dim roll As New Random()
Dim intRoll = roll.Next(1, 7)
'make the current counter visible
If intScore > 0 Then
strCounterName = "lblCounter" & intScore
Me.Controls(strCounterName).Visible = False
End If
'input
intValue = intRoll
intScore = intScore + intValue
strCounterName = "lblcounter" & intScore.ToString
'output
txtMove.Text = intRoll
Me.Controls(strCounterName).Visible = True
If intScore = 3 Then
lblCounter3.Visible = False
lblCounter22.Visible = True
intScore = 22
End If
If intScore = 5 Then
lblCounter5.Visible = False
lblCounter8.Visible = True
intScore = 8
End If
If intScore = 11 Then
lblCounter11.Visible = False
lblCounter26.Visible = True
intScore = 26
End If
If intScore = 20 Then
lblCounter20.Visible = False
lblCounter29.Visible = True
intScore = 29
End If
If intScore = 17 Then
lblCounter17.Visible = False
lblCounter4.Visible = True
intScore = 4
End If
If intScore = 19 Then
lblCounter19.Visible = False
lblCounter7.Visible = True
intScore = 7
End If
If intScore = 21 Then
lblCounter21.Visible = False
lblCounter9.Visible = True
intScore = 9
End If
If intScore = 27 Then
lblCounter27.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 31 Then
lblCounter31.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 32 Then
lblCounter32.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 33 Then
lblCounter33.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 30 Then
MsgBox("Winner")
End If
If intScore > 30 Then
MsgBox("OvErBoArD")
End If
End Sub
End Class
由于intScore超过30时只有30个计数器,因此将抛出此异常 将If语句添加到输入中,如下所示:
'input
intValue = intRoll
If intscore + intvalue < 30 then
intscore=intscore + intvalue
End if
strCounterName = "lblcounter" & intScore.ToString
”输入
intValue=intRoll
如果intscore+intvalue<30,则
intscore=intscore+intvalue
如果结束
strCounterName=“lblcounter”&intScore.ToString
这样,在设置LBL计数器之前,它不会超过30,也不会出现故障这将向用户显示他们已经检查过,并跳过将滚动添加到总数中,以使其保持原位 更改:
'input
intValue = intRoll
intScore = intScore + intValue
strCounterName = "lblcounter" & intScore.ToString
致:
请注意,
vba
与vb.net
不同。当您添加用户的“掷骰子”时,您需要添加一些检查,以查看如果掷骰子+积分>30,则
不要使用计数器,否则将掷骰子添加到计数器中
。我对这一点不熟悉,我的老师没有太多的解释,抱歉,比约恩除非这是你唯一要写的东西,否则这不会是你第一次遇到NRE。链接上的VB答案可能会有所帮助。这不符合用户的标准“如果滚动的数字超过30,我希望标记保持在原来的位置。”谢谢!这工作做得很好!
'input
intValue = intRoll
If intScore + intValue > 30 Then
MsgBox "OvErBoArD"
Else
intScore = intScore + intValue
End If
strCounterName = "lblcounter" & intScore.ToString