0

I am trying to make a Hangman programm for my Uni and i need some help. The program after One try that goes well. At the end you have the option to play another game or to stop program but it keeps the answer yes asking for a new word. How can i access menu after some tries. Can you help me?

import java.util.Scanner;

public class MainGame {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean found = true;
        boolean playAgain = true;
        int life = 8;

        while (playAgain) {
            System.out.println(" MAIN MENU ");
            System.out.println("- Start a new Hangman Game (S) ");
            System.out.println("- Exit (E) ");
            System.out.println("Please enter your choice : ");

            String answer = sc.nextLine().toUpperCase();

            if (answer.equals("E")) {
                playAgain = false;
            } else {
                System.out.println("Give Word");
                String word = sc.nextLine();
                char[] filler = new char[word.length()];
                int i = 0;
                while (i < word.length()) {
                    filler[i] = '-';
                    i++;
                }

                while (found) {
                    System.out.print("The Random Word is now : ");
                    System.out.println(filler);
                    System.out.print("You have " + life);
                    System.out.println(" Lives left");

                    System.out.print("Your Guess : ");
                    char guess = sc.next().charAt(0);
                    guess = Character.toUpperCase(guess);

                    System.out.println(guess);

                    int j = 0;
                    if (word.contains(guess + "")) {
                        System.out.println("The Guess is Correct !!");
                        for (j = 0; j < word.length(); j++) {
                            if (word.charAt(j) == guess) {
                                filler[j] = guess;
                            }
                        }
                    } else {
                        life--;
                    }

                    if (word.equals(String.valueOf(filler))) {
                        System.out.println("Congratulations You Won! Your Guessed Word is : " + word);
                        found = false;
                    }

                    if (life == 0) {
                        found = false;
                        System.out.println("Game Over.");
                    }
                }
            }
        }
    }
}
user1717259
  • 2,429
  • 5
  • 26
  • 38
John DIm
  • 3
  • 1
  • Have a look at the contents of `answer` after looping. – Phylogenesis Nov 02 '17 at 15:16
  • Can you specify this question a little bit? And next time please use some methods and oop if you´re a student. review and working are much better in OOP way of live. -- watch SOLID and DRY Principle. – LenglBoy Nov 02 '17 at 15:21
  • As another clue. Don't mix calls to `sc.nextLine()` with calls to `sc.next()`. You will not always get the answers you expect. – Phylogenesis Nov 02 '17 at 15:21
  • https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – slim Nov 02 '17 at 17:11

1 Answers1

1
  • used uppercase word.
  • added found = true before loop start.
  • replaced sc.next() sc.nextLine()
    For more info visit Understanding Scanner's nextLine(), next(), and nextInt() methods

            import java.util.Scanner;
    
            public class MainGame {     
    
                public static void main(String[] args) {
                    Scanner sc = new Scanner(System.in);
    
                    boolean found=true;
                    boolean playAgain=true;
                    int life=8;
    
                    while (playAgain) {
    
                        System.out.println(" MAIN MENU ");                          
                        System.out.println ("- Start a new Hangman Game (S) ");
                        System.out.println ("- Exit (E) ");
                        System.out.println ("Please enter your choice : ");
    
                        String answer = sc.nextLine().toUpperCase();   
    
                        if (answer.equals("E")) {
                        playAgain=false;                  
                        }
                        else {
    
                            System.out.println ("Give Word");
    
                            String word = sc.nextLine();
                            word = word.toUpperCase();
                            char[] filler=new char[word.length()];
                            int i =0;
                            while (i<word.length()) {
                                filler[i]='-';
                                i++;
                            }
                            found = true;
                            while (found) {
    
                                System.out.print("The Random Word is now : "); 
                                System.out.println (filler);
                                System.out.print ("You have "+life);
                                System.out.println (" Lives left");
    
                                System.out.print ("Your Guess : ");
                                char guess = sc.nextLine().charAt(0) ;
                                guess = Character.toUpperCase(guess);
    
                                System.out.println (guess);
    
                                int j=0;
                                if (word.contains(guess+"")) {
    
                                    System.out.println ("The Guess is Correct !!");
                                    for (j=0;j<word.length();j++) {
    
                                        if(word.charAt(j)==guess) {
                                            filler[j]=guess;
                                        }
                                    }
                                }
                                else {
                                    life--;
                                }
    
                                if(word.equals(String.valueOf(filler))) {
                                    System.out.println ("Congratulations You Won! Your Guessed Word is : "+word);
                                    found=false;
                                }
    
                                if (life==0) {
                                found=false;
                                System.out.println ("Game Over.");}
                            }               
                        }                   
                    }           
                }
            }
    
Raghu Molabanti
  • 285
  • 3
  • 14