简体   繁体   English

VB.net 表达式不产生值错误

[英]VB.net Expression does not produce a value error

I am brand new to vb.net and programming in general so sorry if this is a basic error but im trying to make ax's and o's game in vb.net and it keeps saying that my CheckForWin does not produce a value which makes zero sense to me and when i looked it up the only questions were about converting different code to vb.net and said nothing about the problem i was having This my code: Public Class Form1我是 vb.net 的新手,一般编程很抱歉,如果这是一个基本错误,但我试图在 vb.net 中制作 ax 和 o 的游戏,它一直说我的 CheckForWin 不会产生一个对我来说意义为零的值,当我查了一下,唯一的问题是关于将不同的代码转换为 vb.net 并且没有提到我遇到的问题这是我的代码:Public Class Form1

Dim Board(9) As String
Dim Player1 As String = "X"
Dim Player2 As String = "O"

Public Property X As String

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

End Sub

Private Sub ResetBoard()
    Do
        If CheckForWin() = True Then
            Board(0) = 0
            Board(1) = 0
            Board(2) = 0
            Board(3) = 0
            Board(4) = 0
            Board(5) = 0
            Board(6) = 0
            Board(7) = 0
            Board(8) = 0

        End If
    Loop Until Board(0) = 0
    Board(1) = 0
    Board(2) = 0
    Board(3) = 0
    Board(4) = 0
    Board(5) = 0
    Board(6) = 0
    Board(7) = 0
    Board(8) = 0
End Sub

Private Sub CheckForWin()
    If "0""3""6" = X Then
        ResetBoard()
    ElseIf "1""4""7" = X Then
        ResetBoard()
    ElseIf "2""5""8" = X Then
        ResetBoard()
    ElseIf "0""1""2" = X Then
        ResetBoard()
    ElseIf "3""4""5" = X Then
        ResetBoard()
    ElseIf "6""7""8" = X Then
        ResetBoard()
    ElseIf "0""4""8" = X Then
        ResetBoard()
    ElseIf "2""4""6" = X Then
        ResetBoard()
    ElseIf "0""3""6" = 0 Then
        ResetBoard()
    ElseIf "1""4""7" = 0 Then
        ResetBoard()
    ElseIf "2""5""8" = 0 Then
        ResetBoard()
    ElseIf "0""1""2" = 0 Then
        ResetBoard()
    ElseIf "3""4""5" = 0 Then
        ResetBoard()
    ElseIf "6""7""8" = 0 Then
        ResetBoard()
    ElseIf "0""4""8" = 0 Then
        ResetBoard()
    ElseIf "2""4""6" = 0 Then
        ResetBoard()
    End If
End Sub

End Class

The problem lies within this line of code: If CheckForWin() = True Then问题出在这行代码中: If CheckForWin() = True Then

This line of code:这行代码:

If CheckForWin() = True Then
    ...
End If

This sets up a conditional check to see if the result of the CheckForWin method is true.这将设置条件检查以查看CheckForWin方法的结果是否为真。 The issue is that the method is a Sub routine and not a Function.问题是该方法是一个 Sub 例程,而不是 Function。

A Function represents a block of code that returns a value whereas a Sub also represents a block of code but it does not return a value. Function 表示一个返回值的代码块,而 Sub 也表示一个代码块,但它不返回值。

What's more is that it appears as if you have fallen into an infinite loop.更重要的是,它看起来好像您陷入了无限循环。 ResetBoard calls CheckForWin which calls ResetBoard which calls... and so on. ResetBoard调用CheckForWin ,后者调用ResetBoard ,后者调用...等等。

The simplest thing to do would be to:最简单的做法是:

  1. Convert the CheckForWin to a FunctionCheckForWin转换为 Function
  2. Stop calling ResetBoard in the CheckForWin method停止在CheckForWin方法中调用ResetBoard
  3. Return True when any of your conditional checks in the ResetBoard method is TrueResetBoard方法中的任何条件检查为 True 时返回 True

Here is an example:这是一个例子:

Private Sub ResetBoard()
    ' check if there is a winner, if not then stop execution of the method early
    Dim isWin = CheckForWin()
    If (Not isWin) Then
        Return
    End If

    For index = 1 To 8
        Board(index) = 0
    Next
End Sub

Private Function CheckForWin() As Boolean
    Dim winningPatterns = {
        "0""3""6",
        "1""4""7",
        "2""5""8",
        ' etc...
    }
    Dim doAnyMatch = winningPatterns.Contains(X)
    Return doAnyMatch
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM