2013-07-08 53 views
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 
+1

请提供'mess'和'win'数组的内容。 –

+0

你也可以缩短默认的代码设置标志为真,并且只检查是否不乱(i)= win(i) –

+3

在If和step中加入一个断点。您可能会遇到字符串填充问题,即其中一个字符串具有尾随空格。 – Rikalous

回答

0

用空格最有可能的字符串填充是您的问题(例如:与前/后间隔填充任何混乱(i)或赢(I))。请检查字符串内容阵列中或如果你正在编写一个代码只把它定义为赢与“Trim(mess(i)

过滤它当混乱(i)所有项目比赛胜利(I), 请使用下面的代码:

代码#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 

如果你试图写一个代码,找出至少在混乱(i)与赢(我)一个匹配的项目,从而将其定义as'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 
0

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