2016-11-01 15 views
-4

我有所有的方法都能正常工作,但我对如何打印剩余猜测的数量以及如何在我的主要方法中打印Hangman方面感到困惑。我需要通过调用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是不应该在其内部都有一个print语句。