2016-01-21 61 views
-3

我有一個學校任務和hang子手,你慢慢地顯示出這個詞,因爲用戶猜測,我遇到了麻煩。Hang子手作業:需要幫助揭露祕密詞

例如,這就是我想要(例如蘋果醬):

猜想: 'A'

顯示: 'A *****一個***'

猜測: 'p'

顯示: '應用程序***一***'

這是我目前有:

猜測: 'A'

顯示: '一個***** ***一個'

猜測: 'P'

顯示:「* PP ***** **」

這裏是我的代碼,如果有幫助:

import java.util.Scanner; 
import java.util.Random; 

public class HangmanAssignment 
{ 
    static Scanner numIn = new Scanner(System.in); 
    static Scanner strIn = new Scanner(System.in); 

    // Used to hold the user's previous guesses into a string. 
    static StringBuffer buffer = new StringBuffer(); 

    public static void main(String[] args) 
    { 
    String category; 
    int wordLength; 
    int position; 
    int lettersRemaining; 
    int totalLives = 10; 
    boolean isGuessInWord; 
    StringBuffer prevGuessedLetters; 
    String word; 
    String displayWord = ""; 
    char guess; 

    category = getCategory(); 
    word = getWord(category); 

    // Gets the length of the word. 
    wordLength = word.length(); 
    lettersRemaining = wordLength; 
    System.out.println("The length of your word is: " + wordLength + " characters."); 

    // Generates as many '*' as long as word's length and stores it in 'displayWord'. 
    for (int i = 0; i < wordLength; i++) 
    { 
     displayWord += "-"; 
     // System.out.println(displayWord); /* Testing */ 
    } 

    while (lettersRemaining > 0 && totalLives > 0) 
    { 
     // Prompts user to guess a letter. 
     System.out.println("Guess a letter (Note: there are " + wordLength + " letters)"); 
     guess = strIn.findWithinHorizon(".", 0).charAt(0); 

     // Checks if the letter guessed in within 'word'. 
     isGuessInWord = (word.indexOf(guess)) != -1; 

     // Checks if the guess is in within 'word'. 
     if (isGuessInWord == false) 
     { 
     totalLives--; 
     System.out.println("Sorry, but '" + guess + "' was not in the word."); 

     if (totalLives < 1) 
     { 
      System.out.println("It seems like you have no lives left! :("); 
     } 
     else if (totalLives == 1) 
     { 
      System.out.println("Careful! You only have 1 life left!"); 
     } 
     else 
     { 
      System.out.println("You still have " + (totalLives) + " lives left!"); 
     } 
     } 

     else 
     { 
     System.out.println("Nice one! The letter '" + guess + "' was in the word!"); 

     for (position = 0; position < wordLength; position++) 
     { 
      String newDisplayWord = ""; 

      for (position = 0; position < wordLength; position++) 
      { 
      if (word.charAt(position) == guess) 
      { 
       /* displayWord.charAt(position).equals(word.charAt(position)); */ 
       System.out.print(guess); 
       lettersRemaining--; 
      } 
      else 
      { 
       System.out.print("*"); 
      } 
      } 
     } 
     } 

     // Holds the user's previously guessed letters and the number of letters in the word that are still unknown. 
     System.out.println(); 
     prevGuessedLetters = buffer.append(guess); 
     System.out.println("Previously guessed letters: " + (prevGuessedLetters)); 
     System.out.println("Letters remaining: " + (lettersRemaining)); 
     System.out.println(""); 

     // Checks win/lose conditions. 
     if (lettersRemaining == 0) 
     { 
     System.out.println("Congratulations, '" + word + "' was the correct answer!"); 
     } 
     if (totalLives < 1) 
     { 
     System.out.println("Sorry, you lose!"); 
     System.out.println("The correct answer was '" + word + "'."); 
     { 
      break; 
     } 
     } 
    } 
    } 

    public static String getCategory() 
    { 
    String category; 

    System.out.println("====================================="); 
    System.out.println("Welcome to the Hangman Game!"); 
    System.out.println("====================================="); 

    while (true) 
    { 
     System.out.println("Choose from the following categories:"); 
     System.out.println("1. Car Brands"); 
     System.out.println("2. Countries"); 
     System.out.println("3. Animals"); 
     System.out.println("4. Fruit"); 
     System.out.println(""); 
     category = strIn.nextLine(); 

     if (category.toLowerCase().equals("1") || (category.toLowerCase().equals("one"))) 
     { 
     category = "car brands"; 
     } 
     else if (category.toLowerCase().equals("2") || (category.toLowerCase().equals("two"))) 
     { 
     category = "countries"; 
     } 
     else if (category.toLowerCase().equals("3") || (category.toLowerCase().equals("three"))) 
     { 
     category = "animals"; 
     } 
     else if (category.toLowerCase().equals("4") || (category.toLowerCase().equals("four"))) 
     { 
     category = "fruit"; 
     } 

     if (category.toLowerCase().equals("car brands") || category.toLowerCase().equals("countries") || category.toLowerCase().equals("animals") || category.toLowerCase().equals("fruit")) 
     { 
     System.out.println(""); 
     System.out.println("Nice Choice! You have chosen the '" + category + "' category!"); 
     System.out.println(""); 
     break; 
     } 
     else 
     { 
     System.out.println("Sorry, but '" + category + "' is not a valid input. Try Again!"); 
     System.out.println(""); 
     } 
    } 
    return category; 
    } 

    public static String getWord(String category) 
    { 
    String[] carBrandsWord = {"toyota", "ferrari", "honda", "hyundai", "lamborghini", "dodge", "ford", "chevrolet", "fiat", "lexus", "volkswagen", "acura", "audi", "bentley", "bugatti", "buick", "cadillac"}; 
    String[] countriesWord = {"canada", "england", "france", "switzerland", "australia", "sweden", "greece", "italy", "mexico", "brazil", "india", "china", "russia", "japan", "spain", "ireland"}; 
    String[] animalsWord = {"cat", "dog", "parrot", "bear", "tiger", "monkey", "zebra", "hippopotamus", "chicken", "horse", "cow", "starfish", "squid", "wolf", "hyena", "cheetah", "penguin"}; 
    String[] fruitsWord = {"apple", "banana", "orange", "grapes", "grapefruit", "apricot", "cherry", "guava", "kiwi", "mango", "melon", "olive", "pineapple", "strawberry", "watermelon"}; 

    String word = ""; 

    if (category.toLowerCase().equals("car brands")) 
    { 
     Random random = new Random(); 
     int index = random.nextInt(carBrandsWord.length); 
     word = (carBrandsWord[index]); 
    } 

    else if (category.toLowerCase().equals("countries")) 
    { 
     Random random = new Random(); 
     int index = random.nextInt(countriesWord.length); 
     word = (countriesWord[index]); 
    } 

    else if (category.toLowerCase().equals("animals")) 
    { 
     Random random = new Random(); 
     int index = random.nextInt(animalsWord.length); 
     word = (animalsWord[index]); 
    } 

    else 
    { 
     Random random = new Random(); 
     int index = random.nextInt(fruitsWord.length); 
     word = (fruitsWord[index]); 
    } 

    return word; 
    } 
} 
+2

什麼是你的問題? – intboolstring

+1

還沒有讀取您的代碼。但是在每個提示字母的方法運行之後,都要替換字符串。 – hs2345

回答

0

在main方法喲u需要申報下字的另一個變量將保持字符序列顯示的最後狀態猜字母,如:

String displayWord; 

該變量需要在每場比賽開始時用****來initialiased字大小;

之後,你需要更改代碼這樣的:

else 
    { 
    System.out.println("Nice one! The letter '" + guess + "' was in the word!"); 
    for (position = 0; position < wordLength; position++) 
    { 
     for (position = 0; position < wordLength; position++) 
     { 
     if (word.charAt(position) == guess) 
     { 
      //replacing every time * with guessed letter 
      displayWord = displayWord.substring(0,position)+ guess +displayWord.substring(position + 1); 
      lettersRemaining--; 
     } 
     } 
    } 
    System.out.print(displayWord); // here we display it 
    } 
+0

它對你有幫助嗎? –

+0

是的,它幫助了很多,我現在明白我應該做什麼。謝謝你,感謝它! –

+0

祝你好運,男人:) –

0

只是要使用數組:

import java.util.Scanner; 
import java.util.Random; 

public class HangmanAssignment 
{ 
    static Scanner numIn = new Scanner(System.in); 
    static Scanner strIn = new Scanner(System.in); 

    // Used to hold the user's previous guesses into a string. 
    static StringBuffer buffer = new StringBuffer(); 

    public static void main(String[] args) 
    { 
    String category, word, displayWord = ""; 
    int wordLength, position, lettersRemaining, totalLives = 10; 
    boolean isGuessInWord; 
    char guess; 
    StringBuffer prevGuessedLetters; 
    char temp []= new char [20]; 
    category = getCategory(); 
    word = getWord(category); 

    // Gets the length of the word. 
    wordLength = word.length(); 
    for(int i =0; i <wordLength;i++) 
     temp[i]='*'; 
    lettersRemaining = wordLength; 
    System.out.println("The length of your word is: " + wordLength + " characters."); 

    // Generates as many '*' as long as word's length and stores it in 'displayWord'. 
    for (int i = 0; i < wordLength; i++) 
    { 
     displayWord += "-"; 
     // System.out.println(displayWord); /* Testing */ 
    } 


while (lettersRemaining > 0 && totalLives > 0) 
    { 
     // Prompts user to guess a letter. 
     System.out.println("Guess a letter (Note: there are " + wordLength + " letters)"); 
     guess = strIn.findWithinHorizon(".", 0).charAt(0); 

     // Checks if the letter guessed in within 'word'. 
     isGuessInWord = (word.indexOf(guess)) != -1; 

     // Checks if the guess is in within 'word'. 
     if (isGuessInWord == false) 
     { 
     totalLives--; 
     System.out.println("Sorry, but '" + guess + "' was not in the word."); 

     if (totalLives < 1) 
     { 
      System.out.println("It seems like you have no lives left! :("); 
     } 
     else if (totalLives == 1) 
     { 
      System.out.println("Careful! You only have 1 life left!"); 
     } 
     else 
     { 
      System.out.println("You still have " + (totalLives) + " lives left!"); 
     } 
     } 


     else 
     { 
     System.out.println("Nice one! The letter '" + guess + "' was in the word!"); 

     for (position = 0; position < wordLength; position++) 
     { 


      for (position = 0; position < wordLength; position++) 
      { 
      if (word.charAt(position) == guess) 
      { 
      temp[position]=guess; 
       /* displayWord.charAt(position).equals(word.charAt(position)); */ 

       lettersRemaining--; 
      } 



      } 
     } 
     } 
for(int i = 0;i<wordLength;i++){ 
    System.out.print(temp[i]); 
} 
     // Holds the user's previously guessed letters and the number of letters in the word that are still unknown. 
     System.out.println(); 
     prevGuessedLetters = buffer.append(guess); 
     System.out.println("Previously guessed letters: " + (prevGuessedLetters)); 
     System.out.println("Letters remaining: " + (lettersRemaining)); 
     System.out.println(""); 

     // Checks win/lose conditions. 
     if (lettersRemaining == 0) 
     { 
     System.out.println("Congratulations, '" + word + "' was the correct answer!"); 
     } 
     if (totalLives < 1) 
     { 
     System.out.println("Sorry, you lose!"); 
     System.out.println("The correct answer was '" + word + "'."); 
     { 
      break; 
     } 
     } 
    } 
    } 

    public static String getCategory() 
    { 
    String category; 

    System.out.println("====================================="); 
    System.out.println("Welcome to the Hangman Game!"); 
    System.out.println("====================================="); 

    while (true) 
    { 
     System.out.println("Choose from the following categories:"); 
     System.out.println("1. Car Brands"); 
     System.out.println("2. Countries"); 
     System.out.println("3. Animals"); 
     System.out.println("4. Fruit"); 
     System.out.println(""); 
     category = strIn.nextLine(); 

     if (category.toLowerCase().equals("1") || (category.toLowerCase().equals("one"))) 
     { 
     category = "car brands"; 
     } 
     else if (category.toLowerCase().equals("2") || (category.toLowerCase().equals("two"))) 
     { 
     category = "countries"; 
     } 
     else if (category.toLowerCase().equals("3") || (category.toLowerCase().equals("three"))) 
     { 
     category = "animals"; 
     } 
     else if (category.toLowerCase().equals("4") || (category.toLowerCase().equals("four"))) 
     { 
     category = "fruit"; 
     } 

     if (category.toLowerCase().equals("car brands") || category.toLowerCase().equals("countries") || category.toLowerCase().equals("animals") || category.toLowerCase().equals("fruit")) 
     { 
     System.out.println(""); 
     System.out.println("Nice Choice! You have chosen the '" + category + "' category!"); 
     System.out.println(""); 
     break; 
     } 
     else 
     { 
     System.out.println("Sorry, but '" + category + "' is not a valid input. Try Again!"); 
     System.out.println(""); 
     } 
    } 
    return category; 
    } 

    public static String getWord(String category) 
    { 
    String[] carBrandsWord = {"toyota", "ferrari", "honda", "hyundai", "lamborghini", "dodge", "ford", "chevrolet", "fiat", "lexus", "volkswagen", "acura", "audi", "bentley", "bugatti", "buick", "cadillac"}; 
    String[] countriesWord = {"canada", "england", "france", "switzerland", "australia", "sweden", "greece", "italy", "mexico", "brazil", "india", "china", "russia", "japan", "spain", "ireland"}; 
    String[] animalsWord = {"cat", "dog", "parrot", "bear", "tiger", "monkey", "zebra", "hippopotamus", "chicken", "horse", "cow", "starfish", "squid", "wolf", "hyena", "cheetah", "penguin"}; 
    String[] fruitsWord = {"apple", "banana", "orange", "grapes", "grapefruit", "apricot", "cherry", "guava", "kiwi", "mango", "melon", "olive", "pineapple", "strawberry", "watermelon"}; 

    String word = ""; 

    if (category.toLowerCase().equals("car brands")) 
    { 
     Random random = new Random(); 
     int index = random.nextInt(carBrandsWord.length); 
     word = (carBrandsWord[index]); 
    } 

    else if (category.toLowerCase().equals("countries")) 
    { 
     Random random = new Random(); 
     int index = random.nextInt(countriesWord.length); 
     word = (countriesWord[index]); 
    } 

    else if (category.toLowerCase().equals("animals")) 
    { 
     Random random = new Random(); 
     int index = random.nextInt(animalsWord.length); 
     word = (animalsWord[index]); 
    } 

    else 
    { 
     Random random = new Random(); 
     int index = random.nextInt(fruitsWord.length); 
     word = (fruitsWord[index]); 
    } 

    return word; 
    } 
} 
+0

不要忘記清理你的代碼,擺脫未使用的變量,等等...... – Arsaceus