由于标签,vb.net 中的 if 语句中的语法错误

Syntax error in if statement in vb.net because of label

提问人:Dylaka83 提问时间:2/2/2023 最后编辑:Idle_MindDylaka83 更新时间:2/2/2023 访问量:65

问:

我是 vb.net 和编程的新手,如果这是一个愚蠢的问题,我很抱歉,但我正在制作一个 x 和 o 的游戏,我试图显示轮到谁了,但我的 if 语句一直说我有一个语法错误,但它没有具体说明出了什么问题或如何解决它

法典:

Option Explicit On
Public Class Form1

    Dim Board(8) As String
    Dim Player1 As String = "X"
    Dim Player2 As String = "O"
    Dim XScore As String = ""
    Dim WhosTurn As String = Player1

    Public Property X As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ResetBoard()
    End Sub

    Private Sub ResetBoard()
        WhosTurn = Player1
        For i = 0 To 8
            Board(i) = ""
        Next
        If WhosTurn = Player1 Then
            lblPlayerTurn.Text = "It is player one's turn"
        ElseIf WhosTurn <> Player1 Then
            End
        End If

        lblScorePlayer1X.Text = "Player 1 score"
        lblPlayer2O.Text = "Player 2 score"
        lblP1ScoreNumber.Text = 0
        lblP2ScoreNumber.Text = 0
        'If WhosTurn = Player1 Then
        'lblPlayerTurn.Text = "It is player one's turn"
        'ElseIf WhosTurn = Player2 Then
        'lblPlayerTurn.Text = "It is player two's turn"
        'End If
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""

    End Sub

    Private Function CheckForWin(ByRef PlayerToCheck As String) As Boolean



        'Player1 wins
        If (Board(0) = PlayerToCheck And Board(3) = PlayerToCheck And Board(6) = PlayerToCheck) Then
            Return True
        ElseIf (Board(1) = PlayerToCheck And Board(4) = PlayerToCheck And Board(7) = PlayerToCheck) Then
            Return True
        ElseIf (Board(2) = PlayerToCheck And Board(5) = PlayerToCheck And Board(8) = PlayerToCheck) Then
            Return True
        ElseIf (Board(0) = PlayerToCheck And Board(1) = PlayerToCheck And Board(2) = PlayerToCheck) Then
            Return True
        ElseIf (Board(3) = PlayerToCheck And Board(4) = PlayerToCheck And Board(5) = PlayerToCheck) Then
            Return True
        ElseIf (Board(6) = PlayerToCheck And Board(7) = PlayerToCheck And Board(8) = PlayerToCheck) Then
            Return True
        ElseIf (Board(0) = PlayerToCheck And Board(4) = PlayerToCheck And Board(8) = PlayerToCheck) Then
            Return True
        ElseIf (Board(2) = PlayerToCheck And Board(4) = PlayerToCheck And Board(6) = PlayerToCheck) Then
            Return True
        End If

        Return False




    End Function





    Private Sub TakeTurn(ByVal Position As Integer)


        If (WhosTurn = Player1) Then
            Board(Position) = Player1
        Else
            Board(Position) = Player2

        End If
        'Check for win is here if you get confused 
        If (CheckForWin(WhosTurn) = True) Then
            MessageBox.Show("Player " & WhosTurn & " Won")
            ResetBoard()
            If WhosTurn = X Then
                lblP1ScoreNumber + 1 
            End If
            Return
            End If

            If (WhosTurn = Player1) Then

            WhosTurn = Player2
        Else

            WhosTurn = Player1
        End If

        If WhosTurn = Player2 Then
            lblPlayerTurn.Text = "It is player two's turn"
        ElseIf WhosTurn = Player1 Then
            lblPlayerTurn.Text = "It is player one's turn"
        End If
        'You've got that code wired to the label's on click event. Is that intended?
        'Shouldn 't it be at the bottom of the TakeTurn sub? Looks almost like you could replace 
        'that If statement and just set the label directly there instead of WhosTurn 
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Board(0) <> "" Then
            Return
        End If
        Button1.Text = WhosTurn
        TakeTurn(0)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If Board(1) <> "" Then
            Return
        End If
        Button2.Text = WhosTurn
        TakeTurn(1)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If Board(2) <> "" Then
            Return
        End If
        Button3.Text = WhosTurn
        TakeTurn(2)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If Board(3) <> "" Then
            Return
        End If
        Button4.Text = WhosTurn
        TakeTurn(3)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If Board(4) <> "" Then
            Return
        End If
        Button5.Text = WhosTurn
        TakeTurn(4)
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If Board(5) <> "" Then
            Return
        End If
        Button6.Text = WhosTurn
        TakeTurn(5)
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        If Board(6) <> "" Then
            Return
        End If
        Button7.Text = WhosTurn
        TakeTurn(6)
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If Board(7) <> "" Then
            Return
        End If
        Button8.Text = WhosTurn
        TakeTurn(7)
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        If Board(8) <> "" Then
            Return
        End If
        Button9.Text = WhosTurn
        TakeTurn(8)
    End Sub

    Private Sub LabelTurn()

        If WhosTurn = Player1 Then
            lblPlayerTurn.Text = Player1
        Else
            lblPlayerTurn.Text = Player2
        End If


    End Sub
End Class

问题出在这行代码中:

(lblPlayerTurn) Text = Player1

与 P2 相同

我试图去掉括号,但后来它说“方法参数必须括在括号中”和“太多的参数到'Friend WithEvents lblPlayerTurn as Label'”,所以这就是我尝试在括号中关闭 lblPlayerTurn 的原因

vb.net if 语句 标签 语法错误

评论

1赞 Craig 2/2/2023
应该是 .不知道为什么你认为它应该在parens中或不应该有点?lblPlayerTurn.Text = ...
0赞 Hursey 2/2/2023
只有 lblPlayerTurn.Text 有什么问题?
0赞 Dylaka83 2/2/2023
@Hursey我尝试过语法错误现在消失了,但是当我运行它时没有显示单词
1赞 Hursey 2/2/2023
您已将该代码连接到标签的点击事件。这是故意的吗?它不应该在 TakeTurn 潜艇的底部吗?看起来您几乎可以替换该 if 语句,然后直接在那里设置标签而不是 WhosTurn
1赞 Idle_Mind 2/2/2023
您应该解决显然来自同一项目的上一个问题

答: 暂无答案