Hello everybody,

I'm stuck with my Java program. This is the point. When a user had tried for nine times, the user has the choice to restart Hangman or close the program.

But when they hit the y or n key during the game, it's just one of the 26 characters.

I have add the code of my main.java

Thanks in advance for all your help :)

Kind regards,

MeandJava

package hangman;

/*
 * Author Patrick
 */

class Galg {

    public void print(int i) {


        switch(i) {

        case 0:

        System.out.println("____________________________________________________________________");
        System.out.println(" *       *     *     *     * ********* *       *     *     *      * ");
        System.out.println(" *       *    * *    * *   * *         * *   * *    * *    * *    * ");
        System.out.println(" *********   *****   *   * * *     *** *   *   *   *****   *   *  * ");
        System.out.println(" *       *  *     *  *    ** *       * *       *  *     *  *     ** ");
        System.out.println(" *       * *       * *     * ********* *       * *       * *      * ");
        System.out.println("____________________________________________________________________");

        System.out.println("Press q to quit this game.");

        System.out.println("      ");
        System.out.println("|     ");
        System.out.println("|     ");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 1:
        System.out.println("_____ ");
        System.out.println("|     ");
        System.out.println("|     ");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 2:
        System.out.println("_____ ");
        System.out.println("|/    ");
        System.out.println("|     ");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 3:
        System.out.println("_____ ");
        System.out.println("|/  | ");
        System.out.println("|     ");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 4:
        System.out.println("_____ ");
        System.out.println("|/  | ");
        System.out.println("|   0 ");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 5:
        System.out.println("_____ ");
        System.out.println("|/  | ");
        System.out.println("|   0 ");
        System.out.println("|   | ");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 6:
        System.out.println("_____ ");
        System.out.println("|/  | ");
        System.out.println("|   0 ");
        System.out.println("|  /| ");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 7:
        System.out.println("_____ ");
        System.out.println("|/  | ");
        System.out.println("|   0 ");
        System.out.println("|  /|\\");
        System.out.println("|     ");
        System.out.println("| _ _ ");
        break;

        case 8:
        System.out.println("_____ ");
        System.out.println("|/  | ");
        System.out.println("|   0 ");
        System.out.println("|  /|\\");
        System.out.println("|  /  ");
        System.out.println("| _ _ ");
        break;

        case 9:
        System.out.println("_____ ");
        System.out.println("|/  | ");
        System.out.println("|   0 ");
        System.out.println("|  /|\\");
        System.out.println("|  / \\");
        System.out.println("| _ _ ");

        System.out.println();

        System.out.println(" _____________________________________________________________________________________ ");
        System.out.println("| *********     *     *       * *********   ********* *       * ********* *********   |");
        System.out.println("| *            * *    * *   * * *           *       *  *     *  *         *       *   |");
        System.out.println("| *     ***   *****   *   *   * ******      *       *   *   *   ******    *********   |");
        System.out.println("| *       *  *     *  *       * *           *       *    * *    *         *        *  |");
        System.out.println("| ********* *       * *       * *********   *********     *     ********* *         * |");
        System.out.println("|_____________________________________________________________________________________|");
        }
    }
}

public class Main {

    public static void main(String args[])
        throws java.io.IOException {
        
         // Loops stays running when the right characters are not guessed.
       
       GuessedLetters GL = new GuessedLetters();
       
       Galg g = new Galg(); //New object Galg
            
       Word h = new Word(); // New object Word
           
            int wrong = 0;
            int tries = 0;
            

       for(;;) {

            tries++;
            g.print(wrong);
            System.out.println();

            char keuzes = 0;

        System.out.println("Next Letter: ");

     
        
        while (keuzes < 'a' | keuzes > 'z'){
        keuzes = (char) System.in.read();
        }
          
        

        GL.add(keuzes);
        GL.print();
        
        
        
        boolean geraden = h.processGuess(keuzes);

         if(h.hasBeenGuessed() == true) {
             
                System.out.println("Congratulations! You guessed the word! \nWanna try again press yes: y or no: n.");
                break;
         }


               
             
        h.print();
             System.out.println("********************************************************************");

            if (keuzes == 'q') {
                break;
         
            }

                
            if(geraden == false) {
                    wrong++;       
             }

             if(tries == 9){
                 System.out.println("You are dead! \nWanna try again? Press yes: y or no: n");
                 
                 while (keuzes == 'y' | keuzes == 'n'){
                 keuzes = (char) System.in.read();
                 }

                 if(keuzes == 'y') {

                    System.out.println("Hangman will restart. Wait a second...");
                    wrong = 0;
                    tries = 0;
                    
                    continue;
                 }

                 if(keuzes == 'n'){

                   System.out.println("Thanks for playing. See you next time!");
                   break;
                 }
                

             } 

             
         }
     }
}

Just put the whole thing in a while loop.

while (true)
{
    // Program here

    // Ask user if they want to continue
    // If yes, do nothing. If no, "break" out of while loop.
}

// Any code you want to execute before the program ends goes here

I think this line will get the last value of keuzes inside your game

while (keuzes == 'y' | keuzes == 'n'){ 
     keuzes = (char) System.in.read();                 
}

so why not, ask for the user first so that keuzes will be initialized to the letter the user inputted before you do your while loop.

keuzes = (char) System.in.read()

while (keuzes == 'y' | keuzes == 'n'){ 
     keuzes = (char) System.in.read();                 
}
for(;;) {

tries++;

(...)

 if(tries == 9){
...
}

Um, if you want to go around nine times, what was wrong with

for (int tries = 0; tries<9; tries++)
{
....
}

Of course, the kinder thing to do would be to ask each time, as leiger says. You can use the infinite loop and break, as he suggests, but better style says put the conditions where someone expects to find them.

done =false;
while (!done)
{
  // game code
  // if they say they're done, set done to true
}

Of course, the kinder thing to do would be to ask each time, as leiger says. You can use the infinite loop and break, as he suggests, but better style says put the conditions where someone expects to find them.

done =false;
while (!done)
{
  // game code
  // if they say they're done, set done to true
}

I'm the first to admit that my code isn't always the best way of doing something :)

Your solution is easier to understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.