Help with if statement or counter (not sure which)

• October 7th, 2013, 10:21 AM
tonynsx
Help with if statement or counter (not sure which)
In the program below, I'm trying to count the number of times the wrong letter is guessed, and then count it by one. My problem is that, even if the correct letter is guessed, it counts. How can I fix this? I've tried everything, but no clue, so I thought I would ask here.

Thanks,

Tony

Code :

```package hangmangame;   /* * STEPS TO WRIE HANGMAN PROGRAM (Hangman game pseudocode) * ------------------------------------------------------- * 1. Create an array with some words * 2. Create an empty dashArray * 3. Create an alphabet array to delete used letters * 4. Get a random word from the word array using random number generator * 5. Set the word to the random number's index from the word array * 6. Create a new dash array with the number of letters in the word * 7. Get a guess from user * 8. Check if the guess is in the word * 9. If yes, replace the dash with the correct letter * 10. Delete the letter from alphabet array (good or bad) * 11. If the correct word is guessed, show the word and says you win * 12. If the incorrect word is guessed, only take the first character of the word, and put it in the correct place * 13. If the correct word is guessed by letter by letter before counter is more than six, player wins * 14. Create a counter to count each time the wrong guess is entered * 15. Display the appropriate "hangman" image * 16. If the counter is more than six, the player fails (hangman hanged) * * Need to figure out the counter to display each hangman sequence */   import java.util.Random; import java.util.Scanner;   public class HangmanGame {   public static void main(String[] args) {   Random rand = new Random(); Scanner scan = new Scanner(System.in);   String word, guess, convertDashArray; char check; int random = 0, counter = 0; boolean hanged = false;   String[] alphabetArray = {" A ", " B ", " C ", " D ", " E ", " F ", " G ", " H ", " I ", " J ", " K ", " L ", " M ", " N ", " 0 ", " P ", " Q ", " R ", " S ", " T ", " U ", " V ", " W ", " X ", " Y ", " Z "};   String[] wordsArray = {"elephant"};   /* , "star", " candy", "wings", "shopping", "steak", "couch", "toy", "cat", "mouse", "elevator", "taxi", "airplane", "bus", "car", "flower", "cheese", "apple", "orange", "coat", "running", "sweater", "blanket", "chocolate", "chicken", "computer", "rice", "dance", "doll", "duck", "alligator", "snake", "calculator", "castle", "balloon", "laptop", "turtle", "pencil", "fridge", "police", "antartica", "dinner", "fire", "cookie", "pizza", "church", "village" */   char[] dashArray;   random = rand.nextInt(wordsArray.length); word = wordsArray[random];   dashArray = new char[word.length()];   System.out.println("------------------: HANGMAN GAME INSTRUCTIONS :------------------"); System.out.println("1. You can enter either a letter or try to guess the word any time."); System.out.println("2. You have six chances before the hangman is hanged (you fail)."); System.out.println("3. If you try to guess the word, but the guess is wrong, program will read the first letter only."); System.out.println("4. If you guess the word correctly, you win.");   System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|");     System.out.print("Letters available: ");   for(int c=0; c<alphabetArray.length; c++) { System.out.print(alphabetArray[c]); }   System.out.println();   for(int a=0; a<dashArray.length; a++) { dashArray[a] = '-'; System.out.print(dashArray[a]); }   while(hanged == false) { System.out.println();   System.out.print("Enter a guess (letter or word): "); guess = scan.nextLine(); check = guess.charAt(0);   for(int b=0; b<dashArray.length; b++) { if(check == word.charAt(b)) { alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";   dashArray[b] = guess.charAt(0); }   else { alphabetArray[(int) Character.toUpperCase(check) - 65] = " ";   hanged = false; }   System.out.print(dashArray[b]); }   if(hanged == false) { counter++; }   System.out.println("Counter is " + counter);   convertDashArray = new String(dashArray);   if(word.equals(guess) || convertDashArray.equals(word)) { // dashArray[b] = guess.charAt(b);   hanged = true; }   System.out.println();   if(counter == 1) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; }   else if(counter == 2) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; }   else if(counter == 3) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/| |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; }   else if(counter == 4) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/|\\ |"); System.out.println("\t\t\t\t\t |"); System.out.println("\t\t\t\t\t_____|"); hanged = false;   }   else if(counter == 5) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/|\\ |"); System.out.println("\t\t\t\t\t/ |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; }   else if(counter == 6) { System.out.println("\t\t\t\t\t _____"); System.out.println("\t\t\t\t\t | |"); System.out.println("\t\t\t\t\t 0 |"); System.out.println("\t\t\t\t\t/|\\ |"); System.out.println("\t\t\t\t\t/ \\ |"); System.out.println("\t\t\t\t\t_____|"); hanged = false; }   if(counter > 6) { System.out.println("The word was: " + word); hanged = true; }   if(hanged == true) { System.out.println("Good guess, you win!"); }   System.out.println();   System.out.print("Letters available: ");   for(int d=0; d<alphabetArray.length; d++) { switch (guess) { case "a": alphabetArray[0] = " "; break; case "b": alphabetArray[1] = " "; break; case "c": alphabetArray[2] = " "; break; case "d": alphabetArray[3] = " "; break; case "e": alphabetArray[4] = " "; break; case "f": alphabetArray[5] = " "; break; case "g": alphabetArray[6] = " "; break; case "h": alphabetArray[7] = " "; break; case "i": alphabetArray[8] = " "; break; case "j": alphabetArray[9] = " "; break; case "k": alphabetArray[10] = " "; break; case "l": alphabetArray[11] = " "; break; case "m": alphabetArray[12] = " "; break; case "n": alphabetArray[13] = " "; break; case "o": alphabetArray[14] = " "; break; case "p": alphabetArray[15] = " "; break; case "q": alphabetArray[16] = " "; break; case "r": alphabetArray[17] = " "; break; case "s": alphabetArray[18] = " "; break; case "t": alphabetArray[19] = " "; break; case "u": alphabetArray[20] = " "; break; case "v": alphabetArray[21] = " "; break; case "w": alphabetArray[22] = " "; break; case "x": alphabetArray[23] = " "; break; case "y": alphabetArray[24] = " "; break; case "z": alphabetArray[25] = " "; break; }   System.out.print(alphabetArray[d]); } } } }```
• October 7th, 2013, 01:07 PM
GoodbyeWorld
Re: Help with if statement or counter (not sure which)
if(hanged == false)
{
counter++;
}

System.out.println("Counter is " + counter);

convertDashArray = new String(dashArray);

if(word.equals(guess) || convertDashArray.equals(word))
{
// dashArray[b] = guess.charAt(b);

hanged = true;
}

As there is nothing to set hanged to be true before you have that if check, it'll go to

if (hanged == false) first before it reaches if (word.equals(guess) || convertDashArray.equals(word)), even if the latter returns true.

Try checking to see if it meets the criteria first so hanged can be true before you check to see if hanged is false.