2014-12-05 90 views
-5

我有這個代碼的兩個問題。當用戶猜字母時,我需要爲「船」這個詞顯示_ _ _ _ _。當用戶輸入它們時,需要用猜測的字母替換這些不足。 此外,我還有一個計數器來跟蹤好猜測,這應該在5次猜測後結束遊戲,但是當我運行代碼時不會發生這種情況。任何問題的建議?Hangman java編碼

private static String [] word = {"b","o", "a","t","s"}; // array to hold the secret word 
    private static boolean finished = false; // boolean flag to control the main loop 
    private static int badGuesses = 0;   // int variable to hold the number of incorrect guesses 
    private static ArrayList<String> guessedLetters = new ArrayList<String>();  // String array to mark which letters have been guessed 
    private static String entryWord = " ";   // string variable for the user's guesses 
    private static int goodGuesses = 0;    // int variable to hold the number of correct guesses 
    private static String guess; 

public static void main (String[] args) 
{ 
    playGame(); 

} 


public static void playGame() 
{ 
    displayRules(); // print the instructions to the user 

    while (!finished) 

    { 
     String guess = getGuess(); // allow user to input a guess 
     checkGuess(word, guess); // identify user's guess as correct or incorrect 
     showWord(); // print the underscores representing the word 
     showMan(); // print the appropriate hanging man 
    } 

    while (goodGuesses == 5) 
     winGame(); 
} 

public static void displayRules()  // display the rules to the user 
{ 
    String userName; 
    Scanner keyboard = new Scanner (System.in); 
    System.out.println ("Please enter your name."); 
    userName = keyboard.next(); 


    System.out.println ("Hello " + userName + "!"); 
    System.out.println ("Welcome to Dan's Hangman game!"); 
    System.out.println ("The objective of the game is to guess all of the letters of the secret word"); 
    System.out.println ("The user will guess a letter and if it is correct, the user has won that round."); 
    System.out.println ("If the user guesses an incorrect letter, the user will gain a strike."); 
    System.out.println ("A strike will cause part of the man to be hanged"); 
    System.out.println ("The game will end after the user either gets 6 strikes, or guesses the entire word correctly."); 
    System.out.println ("----------------------------------------------"); 
    System.out.println(" "); 
} 


public static void showMan()   // display the appropriate hanging man based on the number of incorrect guesses 
{ 
    if (badGuesses==0) 
     man_0(); 
    else if (badGuesses==1) 
     man_1(); 
    else if(badGuesses==2) 
     man_2(); 
    else if(badGuesses==3) 
     man_3(); 
    else if(badGuesses==4) 
     man_4(); 
    else if(badGuesses==5) 
     man_5(); 
    else if (badGuesses==6) 
     man_6(); 


} 

public static void showWord()  // Show the status of the word as it is guessed 
{ 



    String s = word.toString(); 
    char [] showWordArray = s.toCharArray(); 
    char c = guess.charAt(0); 
    boolean contains = true; // boolean flag to control the loop 

    for (int i=0; i<word[0].length(); i++) 
    { 
     //showWordArray[i] += '_'; 

     if (showWordArray[i] == c) 
     { 
      System.out.print (c); 
     } 
     else 
      System.out.print ('_'); 
    } 

    System.out.println (" "); 
} 





public static String getGuess()   // allow the user to guess a letter 
{ 

    Scanner keyboard = new Scanner (System.in); 
    System.out.println (" "); 
    System.out.println ("Please enter a guess"); 
    guess = keyboard.nextLine(); 
    return guess; 
} 

public static String checkGuess (String [] word, String guess) // identify if the user's guess if correct or incorrect 
{ 

    int index = 0;  // loop control variable 
    int element= -1; // element the string is found at 
    boolean found= false; // flag indicating search results 


    do 
    { 
     if (word[index].equals(guess)) 
      { 
       System.out.println ("You guessed correctly!"); 
       goodGuesses++; 
       found = true; 

      } 
     index++; 

    } 
    while (index< word.length); 

    if (found == false) 
    { 
     System.out.println ("Sorry, you guessed incorrectly "); 
     badGuesses ++; 
    }   

return Integer.toString (element); 

} 

public static void loseGame() 
{ 
    if (badGuesses == 6) 
     System.out.println ("Sorry, you lost! :("); 
     finished = true; 
} 

public static void winGame() 
{ 
    if (goodGuesses == 5) 
     System.out.println ("Congratulations! You have won!"); 
     finished = true; 
} 


public static void man_0() 
{ 
    System.out.println ("_____"); 
    System.out.println ("| |"); 
    System.out.println ("|"); 
    System.out.println ("|"); 
    System.out.println ("|"); 

    System.out.println ("You have 6 guesses left"); 
} 

public static void man_1() 
{ 
    System.out.println ("_____"); 
    System.out.println ("| |"); 
    System.out.println ("| o"); 
    System.out.println ("|"); 
    System.out.println ("|"); 

    System.out.println ("You have 5 guesses left"); 
} 

public static void man_2() 
{ 
    System.out.println ("_____"); 
    System.out.println ("| |"); 
    System.out.println ("| o"); 
    System.out.println ("| |"); 
    System.out.println ("|"); 

    System.out.println ("You have 4 guesses left"); 
} 

public static void man_3() 
{ 
    System.out.println ("_____"); 
    System.out.println ("| |"); 
    System.out.println ("| o"); 
    System.out.println ("| /|"); 
    System.out.println ("|"); 

    System.out.println ("You have 3 guesses left"); 
} 

public static void man_4() 
{ 

    System.out.println ("_____"); 
    System.out.println ("| |"); 
    System.out.println ("| o"); 
    System.out.println ("| /|\\"); 
    System.out.println ("|"); 

    System.out.println ("You have 2 guesses left"); 
} 

public static void man_5() 
{ 
    System.out.println ("_____"); 
    System.out.println ("| |"); 
    System.out.println ("| o"); 
    System.out.println ("| /|\\"); 
    System.out.println ("| /"); 

    System.out.println ("You have 1 guess left"); 
} 

public static void man_6() 
{ 
    System.out.println ("_____"); 
    System.out.println ("| |"); 
    System.out.println ("| o"); 
    System.out.println ("| /|\\"); 
    System.out.println ("|/\\"); 
    loseGame(); 
} 
} 
+1

如果您具體告訴我們目前發生的事情,想要發生的事情以及迄今爲止的調試結果,那麼您更有可能獲得幫助。 – 2014-12-05 00:25:06

+0

它顯示猜測是否正確或不正確,並顯示一個下劃線來表示單詞。我試過使用數組列表來跟蹤正確的猜測,並將'word'數組中的字母轉換爲char數組並搜索該數組。我也沒有爲我工作 – user4326743 2014-12-05 00:28:46

+0

我不太清楚是什麼導致你的問題(@ JeffreyBosboom的建議肯定會有幫助),但我可以看到一些地方的代碼可以寫得更好。 'found == false'應該是'!found',showMan()中的'if'-elsese'鏈可以更好地作爲'switch'語句,所有這些'man'函數都可以是一個函數,錯誤猜測的數量作爲論點。 – 2014-12-05 00:30:18

回答

0

基本上,你的showWord方法的邏輯是錯誤的。但不是爲你糾正它,我只是想以一個問題的形式給你一個提示。

  • 你的代碼在哪裏測試「猜測」的第二個字符?

另外要注意的是,你的word表示爲String對象的數組是「不自然」。自然表示是String,其中char[]是可能的替代方案。

我之所以提到這一點,主要是因爲您當前的表示選擇使您的代碼比需要更復雜。

+0

我改變該方法是本現在 對(INT I = 0; I user4326743 2014-12-05 01:16:04

+0

是的。這就說得通了。您需要更多地瞭解代碼的邏輯。 – 2014-12-05 01:39:37