2

下面的代码没有显示You Win!

你能帮我找出问题吗?两个数组都是字符串。

Sub checkwin()
    Dim flag As Boolean = False
    For i As Integer = 0 To win.Length - 1
        If mess(i) = win(i) Then
            flag = True
        Else
            flag = False
            Exit For
        End If
    Next
    If flag = True Then
        lbl1.Content = "You Win!!"
        Timer.Stop()
        Dim name As String = txtName.Text
        Dim data As String = "insert into puzzleTable([picName], [name], [moves], [time]) values ('mona','" & name & "','" & counter & "','" & x & "')"
        mySql.executeSqlQuery(data)
    End If
End Sub
4

2 回答 2

0

唯一可能的原因是您的数组中的数据未对齐(我无法说出原因,因为您没有提供足够的信息)。要获得更简洁的答案,请提供这些数组的内容。

于 2013-07-08T15:23:20.597 回答
0

最有可能使用空格填充字符串是您的问题(例如:用前导/尾随空格填充的 mess(i) 或 win(i))。请检查数组中的字符串内容或使用 ' Trim(mess(i)'过滤

如果您编写的代码仅在 mess(i) 中的所有项目都与 win(i) 匹配时将其定义为 win,请使用以下代码:

代码 #1

    Dim flag As Boolean = True    

    'loop will exit and return flag=FASLE if ANY item in mess(i) not match with win(i)
    For i As Integer = 0 To win.Length - 1
        If trim(mess(i)) <> trim(win(i)) Then 'trim added to filter leading/trailing spaces
            flag = False
            Exit For
        End If
    Next

如果您尝试编写代码以使用 win(i) 在 mess(i) 中找到至少一个匹配项,从而将其定义为“You Win”

尝试将“for循环”代码修改为以下内容:

代码 #2

    Dim flag As Boolean = False    

    'loop will exit and return flag=TRUE if ANY item in mess(i) matches win(i)
    'else return flag=FALSE if no match was found in all the item in mess(i)
    For i As Integer = 0 To win.Length - 1
        If trim(mess(i)) = trim(win(i)) Then 'trim added to filter leading/trailing spaces
            flag = True
            Exit For
        End If
    Next
于 2013-07-08T10:27:35.037 回答