2017-02-02 77 views
0

我試圖做一個hang子手遊戲,並輸入「不正確的」,如果用戶選擇了錯誤的字母,但它會繼續重複,因爲randomWord它從數組中選擇單詞我該如何阻止?如何停止消息框重複,因爲數組?

Sub wordGeneration() 
    Dim wordUsed As Array = {"pizza", "noodle", "zombie", "object", "rowin", "running", "elephant", "lion"} 
    Dim random As New Random 

    randomWord = wordUsed(random.Next(0, 8)) 
    Label2.Text = randomWord 
End Sub 

Sub letterInput() 
    For i As Integer = 0 To randomWord.Length - 1 
     If userInput = randomWord(i) Then 
      MessageBox.Show("correct") 
     ElseIf userInput <> randomWord(i) Then 
      MessageBox.Show("incorrect") 
      Label4.Text = counter 
     End If 
    Next 
End Sub 
+1

在'MessageBox.Show'之後添加'Exit For' ...這將打破循環的進一步執行。 – Codexer

+0

您是否在'Label4.Text = counter'之後的'Exit For'之後? – Bugs

+1

VB.NET不是VBA – Plutonix

回答

0

讓VB爲您搜索更簡單快捷。

Sub letterInput() 
    if InStr(randomWord.Length, userInput) > 0 
     MessageBox.Show("correct") 
    Else 
     MessageBox.Show("incorrect") 
     Label4.Text = counter 
    End If 
End Sub 

注意:我假設userInput是單個字符。