jeudi 16 février 2017

Issue with proper increment in a while loop/if statement

My code currently determines if a guess is wrong or correct. If wrong, guess count goes up. When the guess count hits 6 it should exit out of the loop and say "You Lost". The issue is, with <5, when the counter is at 5 and the next guess is correct, it still says "You Lost." I tried setting the loop to <6, however it then does not say "You Lost" until the 7th guess. I am not sure what I am doing wrong, any help is appreciated. (all the string should be letter guessed since it is a return)

Where guess is incremented:

public String updatedHiddenWord(){ 
    boolean wrongGuess = true;     
        if (xxx) {       
            wrongGuess = false;    
        }        
        if (wrongGuess){
        guesses++;
        }  
        return dashes;        
}

Where the if statements and while loop are:

public String guessNotification(){

while(guesses < 5 && correct < word.length()){  

    if(word.indexOf(hv.keyChar)!=-1 && (hv.keyChar >= 'a' && hv.keyChar <= 'z')) {
        letterGuessed = hv.keyChar + " is in the word";
        if(correct == word.length()){
            win = "You win!!";
        }
    }   
    else if(word.indexOf(hv.keyChar)==-1 && (hv.keyChar >= 'a' && hv.keyChar <= 'z')) {     
        notInWord = hv.keyChar + " is not in the word";
    }
    else{
        notValid = "Not a valid letter";
    }
    return letterGuessed;
}
    if(correct == word.length()){
        win = "You win!!";
        guesses = 5;
    }
    else{
        lose = "You Lost!!";
    }
    return letterGuessed;
}

Aucun commentaire:

Enregistrer un commentaire