2016-07-23 63 views
0

我對編程和Java非常新穎(正如你所看到的),並且在編程hang子手時遇到了一個基本問題。我的代碼並不是我所知道的最乾淨的,但我真的很想在我的錯誤邏輯明智的地方輸入一些信息。我的麻煩是讓程序在猜測中正確輸出'hangman display'。我認爲有一個問題,我嘗試了錯誤的++設置。我也無法得到重複的信件正常工作,但不能完全弄清楚。爪哇Hang子手不會正確地循環

import java.util.*; 

public class HangMan 
{ 

public static void main(String[] args) 
{ 
    char[] alphabet = 
    { 
     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 
     'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' 
    }; 
    String[] words = 
    { 
     "javascript", "declaration", "object", "class", "failing" 
    }; 
    int randomIndex = (int) (Math.random() * words.length); 
    String randomWord = words[randomIndex]; 
    char[] explodedWord = explode(randomWord); 
    char[] censoredWord = new char[randomWord.length()]; 
    int triesWrong = 0; 
    Scanner input; 
    char letterGuess; 
    boolean letterFound = false; 

    for (int i = 0; i < randomWord.length(); i++) 
    { 
     censoredWord[i] = '_'; 
    } 

    while (!gameWon(censoredWord, explodedWord)) 
    { 
     hangMan(triesWrong, censoredWord, alphabet); 
     System.out.println("Please guess a letter: "); 
     input = new Scanner(System.in); 
     letterGuess = input.next().trim().charAt(0); 

     char[] repeatedLetters = new char[26]; 

     for (int i = 0; i < alphabet.length; i++) 
     { 
      if (letterGuess == alphabet[i]) 
      { 
       if(letterGuess == repeatedLetters[i]) 
       { 
        System.out.println("You already guessed this letter, please enter another letter."); 
       } 
       else 
       { 
        repeatedLetters[i] = alphabet[i]; 
        alphabet[i] = '_'; 
       } 
      } 
     } 
     for (int i = 0; i < explodedWord.length; i++) 
     { 
      if (letterGuess == explodedWord[i]) 
      { 
       censoredWord[i] = letterGuess; 
       letterFound = true; 
      } 
     } 
     if (!letterFound) 
     { 
      letterFound = false; 
      triesWrong++; 
     } 
    } 
} 

public static char[] explode(String bank) //getWord method for string bank 
{ 
    //create character array called explode that has length of string 
    char[] explodedWord = new char[bank.length()]; 

    for (int i = 0; i < bank.length(); i++) 
    { 
     explodedWord[i] = bank.charAt(i); 
    } 
    return explodedWord; 
} 

public static void hangMan(int n, char[] dashWord, char[] alph) 
{ 
    if (n == 0) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
    } else if (n == 1) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|\t0"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
    } else if (n == 2) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|\t0"); 
     System.out.println("|\t|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
    } else if (n == 3) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|\t0"); 
     System.out.println("|\t|\\"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
    } else if (n == 4) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|\t0"); 
     System.out.println("|  /|\\"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
    } else if (n == 5) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|\t0"); 
     System.out.println("|  /|\\"); 
     System.out.println("|\t|"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("|"); 
    } else if (n == 6) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|\t0"); 
     System.out.println("|  /|\\"); 
     System.out.println("|\t|"); 
     System.out.println("|  /"); 
     System.out.println("|"); 
     System.out.println("|"); 
    } else if (n == 7) 
    { 
     System.out.println("\n\n---------"); 
     System.out.println("|\t|"); 
     System.out.println("|\t0"); 
     System.out.println("|  /|\\"); 
     System.out.println("|\t|"); 
     System.out.println("| /\\"); 
     System.out.println("|"); 
     System.out.println("|"); 
     System.out.println("GAME OVER"); 
     System.exit(0); 
    } 

    System.out.println("\n\n"); 

    for (int i = 0; i < alph.length; i++) 
    { 
     System.out.print(alph[i] + " "); 
    } 

    System.out.println("\n\n"); 

    for (int i = 0; i < dashWord.length; i++) 
    { 
     System.out.print(dashWord[i] + " "); 
    } 

    System.out.println("\n\n"); 
} 

public static boolean gameWon(char[] dashWord, char[] bigWord) 
{  
    if(dashWord.length != bigWord.length) 
    { 
     return false; 
    } 

    for(int i = 0; i < dashWord.length; i++) 
    { 
     if(dashWord[i] != bigWord[i]) 
     { 
      return false; 
     } 
    } 
    System.out.println("Congratulations, you win!"); 

    return true; 
} 

} 
+0

我會將hang子手數字存儲在文本文件中,這將顯着減少代碼量。並增加可讀性。 – martijnn2008

回答

1
if (!letterFound) 
    { 
     letterFound = false; 

如果!letterFound是事實,就意味着letterFound已經是假的。所以之後不需要將其設置爲假。這是一個邏輯缺陷。 我建議將letterFound = false;移動到while循環的開始位置。所以你每個循環都開始新鮮。