2017-11-02 2 views
0

Ich versuche, ein Hangman-Programm für meine Uni zu machen, und ich brauche etwas Hilfe. Das Programm nach einem Versuch geht gut. Am Ende haben Sie die Möglichkeit, ein anderes Spiel zu spielen oder das Programm zu stoppen, aber es bleibt die Antwort ja, fragt nach einem neuen Wort. Wie kann ich nach einigen Versuchen auf das Menü zugreifen? Kannst du mir helfen?Java Hangman-Projekt kann nicht richtig stoppen

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."); 
        } 
       } 
      } 
     } 
    } 
} 
+0

Werfen Sie einen Blick auf den Inhalt von 'answer' nach Looping. – Phylogenesis

+0

Können Sie diese Frage ein wenig spezifizieren? Und beim nächsten Mal benutzen Sie bitte einige Methoden und oop, wenn Sie ein Student sind. Überprüfung und Arbeit sind viel besser in OOP Art zu leben. - Beobachten Sie SOLID und DRY Prinzip. – LenglBoy

+0

Als ein weiterer Hinweis. Mischen Sie Aufrufe von 'sc.nextLine()' nicht mit Aufrufen von 'sc.next()'. Sie werden nicht immer die Antworten bekommen, die Sie erwarten. – Phylogenesis

Antwort

2
  • verwendet Groß word.
  • hinzugefügt found = true vor dem Schleifenstart.
  • ersetzt sc.next() sc.nextLine()
    Für weitere Informationen besuchen 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.");} 
             }    
            }     
           }   
          } 
         }