简体   繁体   中英

How to stop message box repeating because of array?

I'm trying to make a hangman game and to input "incorrect" if the user picks the wrong letter but it will keep repeating because of the randomWord which chooses the word from an array how do I stop this?

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

Simpler and faster to let VB do the search for you.

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

Note: I am assuming userInput is a single character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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