2016-11-01 2 views
-4

모든 메서드가 올바르게 작동하지만 남은 추측 수를 출력하고 행맨을 기본 메서드에 인쇄하는 방법에 대해 혼란 스럽습니다. 나는 printHangman하지만행맨 (Hangman) 메서드 호출

public class Hangman2 
{ 

public static void main(String[] args) 
{ 
    System.out.println("Welcome to HangMan Player 1, Please enter a word. Player 2, Please close your eyes: "); 
    Scanner stdin = new Scanner(System.in); 

    String secretWord = stdin.next(); 

    for (int x = 1; x <= 100; x++) 
    { 
     System.out.println(" "); 
    } 

    System.out.println("Clearing Screen"); 

    System.out.println("The current partial word is: "); 
    String initialWord = createPartialWord(secretWord); 
    System.out.println(""); 
    System.out.println("The current hangman picture is: "); 




} 


    public static String createPartialWord(String secretWord) 
    { 
     String newsecretWord = ""; 
     int wordLength = secretWord.length(); 

     while (wordLength > 0) 
      { 
       newsecretWord = newsecretWord + "-"; 
       System.out.print("-"); 
       wordLength--; 
      } 
     return newsecretWord; 
    } 

    public static String replaceChar(String word, char c, int i) 
    { 

     if(0 < i && i < word.length()) 
     { 
      return word.substring(0, i) + c + word.substring(i + 1); 
     } 

     return word; 
    } 

    public static String updatePartialWord(String partial, String secret, char c) 
    { 
     for (int i = 0; i <= secret.length(); i++) 
     { 
      if (secret.charAt(i) == c) 
      { 

       return replaceChar(partial, c , i); 
      } 

     } 
     return partial; 


    } 

    public static void printHangman(int guessLeft) 
    { 
     String HEAD = " "; 
     String BODY = " "; 
     String LEGS = " "; 
     String LEFTARM = " "; 
     String RIGHTARM = " "; 

     System.out.println("_____"); 
     System.out.println("| |"); 

     if (guessLeft < 6) 
     { 
      HEAD = "()"; 
     } 

     System.out.println("| " + HEAD); 
     if (guessLeft < 5) 
     { 
      BODY = "||"; 
     } 
     if (guessLeft < 4) 
     { 
      LEFTARM = "\\"; 
     } 
     if (guessLeft < 3) 
     { 
      RIGHTARM = "/"; 
     } 

     System.out.println("| " + LEFTARM + BODY + RIGHTARM); 
     if (guessLeft < 2) 
     { 
      LEGS = "/"; 
     } 
     if (guessLeft < 1) 
     { 
      LEGS += "\\"; 
     } 
     System.out.println("| " + LEGS); 
     System.out.println("|_____\n\n\n\n"); 
    } 


} 
+0

''모든 메소드가 올바르게 작동합니다. '''실제로 메소드를 호출 * 할 수 없다면 어떻게 알 수 있습니까? ''추측 카운터없이 호출하는 방법을 모르겠습니다. ''- Java에서 모든 메서드를 호출하는 것과 같은 방식입니다. 메서드가'int'를 필요로한다면'int'를 주어야합니다. 사용자가 시도한 추측 수 (및 재생중인 게임에 대한 잠재적 인 정보)를 추적해야합니다. – David

+0

다른 메서드는 내가 호출했기 때문에 작동하지만'printHangman'을 호출 할 수 없었지만 코드가 교수로부터 제공 되었기 때문에 작동한다는 것을 알았습니다. 그리고 네,'main()'에서 시도한 추측을 어떻게 추적합니까? – MikeG

+0

실제로 그것을 호출하는 방법을 알아 냈습니다 ... 추측 추적기가 필요합니다. – MikeG

답변

0

사람들은이 포럼에 당신을 위해 과제를 작성하지 않을 당신이 일반적으로 좀 더 시도 할 필요가없는 추측 카운터를 호출하는 방법을 잘하지를 호출하여 현재 교수형 집행을 인쇄해야 그냥 "나는 갇혀있다." 말했다되는 것으로, 여기이 시점을지나 얻을 수있는 대략적인 개요는 다음과 같습니다 또한

public static void main(String[] args) 
    { 
     System.out.println("Welcome to HangMan Player 1, Please enter a word. Player 2, Please close your eyes: "); 
     Scanner stdin = new Scanner(System.in); 

     String secretWord = stdin.next(); 

     int guessesLeft = /* Number of guesses at the start */; 
     String partialWord = createPartialWord(secretWord); 

     while (/* You have guesses remaining */) 
     { 
      for (int x = 1; x <= 100; x++) 
      { 
       System.out.println(" "); 
      } 

      System.out.println("Clearing Screen"); 

      System.out.println("The current partial word is: "); 
      System.out.println(partialWord); 
      System.out.println("The current hangman picture is: "); 

      printHangman(guessesLeft); 

      // TODO: Obtain the next letter from the user 
      // TODO: Update the partial word with the letter 
      // TODO: Make it such that you have one less guess remaining 
     } 
    } 

, 나는 createPartialWord이 그 안에 인쇄 문이 안됩니다 확신 해요.