Vb.net 我最后一个if语句检查抽签有什么问题?我能用线圈吗?

Vb.net 我最后一个if语句检查抽签有什么问题?我能用线圈吗?,vb.net,visual-studio-2015,Vb.net,Visual Studio 2015,我正在检查抽签游戏中是否有平局。这是一个用户对用户的tic-tac-toe游戏,它能正确识别赢家。我上一次尝试检查是否有抽签的if语句有问题。在创建循环时,我可以得到任何指导吗?或者如果语句检查是否没有赢家,那么这就是平局。请帮忙。谢谢 公开课表格1 Dim choice As Boolean Dim playerXwins, playerOwins As Integer Private Sub Form1_Load(sender As Object, e As EventArgs) Hand

我正在检查抽签游戏中是否有平局。这是一个用户对用户的tic-tac-toe游戏,它能正确识别赢家。我上一次尝试检查是否有抽签的if语句有问题。在创建循环时,我可以得到任何指导吗?或者如果语句检查是否没有赢家,那么这就是平局。请帮忙。谢谢

公开课表格1

Dim choice As Boolean
Dim playerXwins, playerOwins As Integer

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

    Panel1.Enabled = False
    Button1.Enabled = False
    Button2.Enabled = False
    Button3.Enabled = False
    Button4.Enabled = False
    Button5.Enabled = False
    Button6.Enabled = False
    Button7.Enabled = False
    Button8.Enabled = False
    Button9.Enabled = False
    ExitButton.Enabled = False

End Sub

Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
    Me.Close()
End Sub


Private Sub PlayButton_Click(sender As Object, e As EventArgs) Handles PlayButton.Click

    PlayButton.Enabled = False
    Panel1.Enabled = True
    Button1.Enabled = True
    Button2.Enabled = True
    Button3.Enabled = True
    Button4.Enabled = True
    Button5.Enabled = True
    Button6.Enabled = True
    Button7.Enabled = True
    Button8.Enabled = True
    Button9.Enabled = True
    ExitButton.Enabled = True


End Sub

Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click, Button9.Click

    If choice = False Then
        CType(sender, Button).Text = "X"
        choice = True
    Else
        CType(sender, Button).Text = "O"
        choice = False
    End If
    ChoosingWinner()

End Sub

Private Sub ChoosingWinner()

    If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then
        MsgBox("Player X Won this Round")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins


    ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = "X" Then
        MsgBox("Player X Won Round")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins

    ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = "X" Then
        MsgBox("Player X Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins
    End If

    If Button2.Text = "X" And Button5.Text = "X" And Button8.Text = "X" Then
        MsgBox("Player X Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins
    End If

    If Button3.Text = "X" And Button6.Text = "X" And Button9.Text = "X" Then
        MsgBox("Player X Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins
    End If

    If Button3.Text = "X" And Button5.Text = "X" And Button7.Text = "X" Then
        MsgBox("Player X Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins
    End If

    If Button4.Text = "X" And Button5.Text = "X" And Button6.Text = "X" Then
        MsgBox("Player X Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins
    End If

    If Button7.Text = "X" And Button8.Text = "X" And Button9.Text = "X" Then
        MsgBox("Player X Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerXwins = playerXwins + 1
        Label2.Text = playerXwins
    End If

    If Button1.Text = "O" And Button2.Text = "O" And Button3.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins
    End If

    If Button1.Text = "O" And Button4.Text = "O" And Button7.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins
    End If

    If Button1.Text = "O" And Button5.Text = "O" And Button9.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins
    End If

    If Button2.Text = "O" And Button5.Text = "O" And Button8.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins
    End If


    If Button3.Text = "O" And Button6.Text = "O" And Button9.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins
    End If

    If Button3.Text = "O" And Button5.Text = "O" And Button7.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins
    End If

    If Button4.Text = "O" And Button5.Text = "O" And Button6.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Text = ""
        Button2.Text = ""
        Button3.Text = ""
        Button4.Text = ""
        Button5.Text = ""
        Button6.Text = ""
        Button7.Text = ""
        Button8.Text = ""
        Button9.Text = ""
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins
    End If


    If Button7.Text = "O" And Button8.Text = "O" And Button9.Text = "O" Then
        MsgBox("Player O Won!")
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        Button4.Enabled = False
        Button5.Enabled = False
        Button6.Enabled = False
        Button7.Enabled = False
        Button8.Enabled = False
        Button9.Enabled = False
        playerOwins = playerOwins + 1
        Label1.Text = playerOwins

    End If

    'checking if there is a draw?
    If Button1.Text And Button2.Text And Button3.Text And Button4.Text And Button5.Text And Button6.Text And Button7.Text And Button8.Text And Button9.Text = "X" Or "O" Then
        MsgBox("There is a Draw")
    End If
End Sub

结束类

这不起作用。这不是vb.net语法的工作方式。每个语句必须返回true或false

If Button1.Text And Button2.Text And Button3.Text And Button4.Text And Button5.Text And Button6.Text And Button7.Text And Button8.Text And Button9.Text = "X" Or "O" Then
    MsgBox("There is a Draw")
End If
你必须这么做

If (Button1.Text = "O" Or Button1.Text = "X") And
   (Button2.Text = "O" Or Button2.Text = "X") And
   (Button3.Text = "O" Or Button3.Text = "X") And
   (Button4.Text = "O" Or Button4.Text = "X") And
   (Button5.Text = "O" Or Button5.Text = "X") And
   (Button6.Text = "O" Or Button6.Text = "X") And
   (Button7.Text = "O" Or Button7.Text = "X") And
   (Button8.Text = "O" Or Button8.Text = "X") And
   (Button9.Text = "O" Or Button9.Text = "X")
    MsgBox("There is a Draw")
End If

你是否调试了你的代码以找出问题所在,或者你是否为我们“保存”了它?是的,我调试了它,只要我按下“播放”按钮,它就会启用游戏中单击的按钮。但是,当我添加最后一个if语句时,它甚至不允许用户按下任何按钮。这是我收到的警告:Microsoft.VisualBasic.dll中发生了“System.InvalidCastException”类型的未处理异常。其他信息:从字符串“X”转换键入“Long”无效。调试不仅仅是按按钮,而是一项活动。如果你有错误,你必须在邮件中告诉我们错误和行。帮助您而不是猜测问题是什么。您在
If
之间放置的每个语句都必须计算为真/假。显然
按钮1。文本
的计算结果不是真/假。正如您在前面的If语句中所做的那样,
If Button1.Text=“X”或Button1.Text=“O”和