samedi 23 février 2019

java hangman - counting correct user guesses

I'm building a hangman/word guessing game that uses an array to check if the user input matches a letter in the word (that they are attempting to guess). Trying to keep track of the number of times the user guesses correctly (using the occurence variable), but whenever the code is run it counts twice.

So let's say the word is "alpaca"... User guesses the letter "p". Occurence equals "2" (should be "1")

Why is it counting twice and how do I fix it?

while (indexCount < wordLength) {
                result = word1.indexOf(guess, indexCount);

                if (result != -1) {
                    occurence++;
                    //wordArr[result] = guess + " ";
                }
                indexCount++;
                

            }

P.S. I hope this makes sense as I am not the best at explaining things. I can post the full code if that helps.

Aucun commentaire:

Enregistrer un commentaire