我正在尝试在 VB.NET 中为一个学校项目构建一个囚徒困境游戏,但我发现这种奇怪的行为我不明白。
我有 2 个用于播放器的按钮,“Co-op”和“Defect”都具有相同的代码结构:
Private Sub btnDefect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefect.Click
Oponent()
MovePlayer = "Defect"
If MoveOponent = "Coop" Then
scorePlayer += 50
scoreOponent -= 50
Else
If MoveOponent = "Defect" Then
scorePlayer -= 10
scoreOponent -= 10
End If
End If
scores()
End Sub
我知道 ELSE - IF 是不必要的,但我这样写是为了更清晰、更易读。问题是,如果两个按钮都有那个 ELSE - 如果我收到编译错误并且无法从 Visual Studio 运行游戏(虽然 bin 文件夹中的 .exe 工作正常)。现在,如果我从任一按钮中删除 ELSE - IF并留下常规的 ELSE,它就可以完美运行。但是,如果我从两者中删除它,则会发生相同的错误。
我得到的错误是:
无法复制文件“obj\x86\Debug\PrisonersDilemma.exe”,因为找不到它。囚徒困境
但我寻找那个 .exe 文件,它就在那里!
知道为什么会这样吗?我可以通过只留下一个 ELSE - IF 来让它工作,但我想知道出了什么问题。
这是完整的代码供参考:
Public Class Form1
Dim MovePlayer As String
Dim MoveOponent As String
Dim scorePlayer As Integer
Dim scoreOponent As Integer
Sub scores()
lblscoreOponent.Text = scoreOponent
lblscorePlayer.Text = scorePlayer
End Sub
Sub Oponent()
If MovePlayer = "" Then
MoveOponent = "Coop"
Else
MoveOponent = MovePlayer
End If
lblMoveOponent.Text = MoveOponent
lblMoveOponent.Visible = True
End Sub
Private Sub btnCoop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCoop.Click
Oponent()
MovePlayer = "Coop"
If MoveOponent = "Coop" Then
scorePlayer += 10
scoreOponent += 10
Else
scorePlayer += 50
scoreOponent -= 50
End If
scores()
End Sub
Private Sub btnDefect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefect.Click
Oponent()
MovePlayer = "Defect"
If MoveOponent = "Coop" Then
scorePlayer += 50
scoreOponent -= 50
Else
If MoveOponent = "Defect" Then
scorePlayer -= 10
scoreOponent -= 10
End If
End If
scores()
End Sub
End Class
谢谢!!