2016-05-24 23 views
-3

私はこのハングマンプロジェクトをやっていますが、ほとんど完了していますが、アンダースコアの代わりに正しい文字をすべて表示することはできません。ちょうど入った。助けてください。Java HangMan Game

import java.util.Random; 
public class HangManGames 
{ 

    public static int i; 
    public static int errors = 0; 
    public static String wordList[] = {"alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan","minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma","oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"}; 
    public static boolean guessLetter; 

    public static String mysteryWord; 
    public static int mysteryWordLength; 
    public static int remainingLetters; 
    StringBuffer buffer = new StringBuffer(); 


    public static void Start() 
    { 
     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 static void Error1() 
    { 
     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 static void Error2() 
    { 
     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 static void Error3() 
    { 
     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 static void Error4() 
    { 
     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 static void Error5() 
    { 
     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 static void Error6() 
    { 
     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 static void main(String args[]) 
    { 
     do{ 
      Random random = new Random(); 
      int index = random.nextInt(50); 
      mysteryWord = wordList[index]; 

      mysteryWordLength = mysteryWord.length(); 

      remainingLetters = mysteryWordLength; 
      String result = ""; 
      for (i = 0; i < mysteryWordLength; i++) 
       result += "_ "; 
      System.out.println(result); 
      System.out.println(" "); 
      while (remainingLetters != 0 && errors < 6) { 
       if (errors == 0){ 
        System.out.println(""); 
        Start(); 
        System.out.println("");} 
       else if (errors == 1){ 
        System.out.println(""); 
        Error1(); 
        System.out.println("");} 
       else if (errors == 2){ 
        System.out.println(""); 
        Error2(); 
        System.out.println("");} 
       else if (errors == 3){ 
        System.out.println(""); 
        Error3(); 
        System.out.println("");} 
       else if (errors == 4){ 
        System.out.println(""); 
        Error4(); 
        System.out.println("");} 
       else if (errors == 5){ 
        System.out.println(""); 
        Error5(); 
        System.out.println("");} 
       else if (errors == 6){ 
        System.out.println(""); 
        Error6(); 
        System.out.println("");} 


       System.out.print("Guess a letter:"); 
       char guess = Expo.enterChar(); 
       guessLetter = (mysteryWord.indexOf(guess)) != -1; 



       if (guessLetter == false) { 
         System.out.println("Sorry, bad guess"); 
         errors++; 
         System.out.print(result); 

        } else { 
         System.out.println("Great guess!"); 
         for (i = 0; i< mysteryWordLength; i++) { 
          if (mysteryWord.charAt(i) == guess) 
           System.out.print(guess); 
          else { 

           remainingLetters--; 
           System.out.print("_"); 
           System.out.print(result.charAt(i)); 

          }       
         }       
       }   
      } 
      }while(remainingLetters != 0 && errors < 6); 

     System.out.println(); 
     System.out.println(); 
     if (remainingLetters == 0 && errors <6) 
     { 
      System.out.print("YAY! You Win!"); 
      System.out.println("The word was " + mysteryWord); 
     }else 
     { 
      System.out.println("Game Over! You Lose!"); 
      System.out.println("The word was " + mysteryWord); 
     } 
    }  

} 
+4

単語のどの部分が既に推測されているかを把握する必要があります。他の変数 'mysteryWordGuessed'のように、推測された文字を置き換えて出力用に使用します。 – mellamokb

答えて

1

(私は文字列を示唆してあなたは、それぞれの推測の後に続いてのいずれか、最初は空の文字列、""、またはArrayList<Character>.あるlettersGuessedという変数を作成しなければならない、またはこの行char guess = Expo.enterChar();はあなたのArrayListまたはStringに文字guessを追加あなたがarraylistに慣れていない場合)lettersGuessed += guess;、次にSystem.out.println("Letters guessed: " + lettersGuessed)です。