2013-07-08 55 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

唯一可能的原因是,在你的陣列中的數據不對齊(我不能說原因這是因爲你沒有提供足夠的信息)。要獲得更簡潔的答案,請提供這些數組的內容。