Java 刽子手任务:需要帮助揭示秘密吗

Java 刽子手任务:需要帮助揭示秘密吗,java,Java,我有一个学校作业,在《刽子手》中,当用户猜到这个词时,你会慢慢地把它透露出来,我对此感到很不舒服 例如,这就是我想要的(例如苹果酱): 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

我有一个学校作业,在《刽子手》中,当用户猜到这个词时,你会慢慢地把它透露出来,我对此感到很不舒服

例如,这就是我想要的(例如苹果酱):

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;
  }
}
猜猜:“a”

显示:“a******a***”

猜猜:“p”

显示:“应用程序***a***”

这就是我目前拥有的:

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;
  }
}
猜猜:“a”

显示:“a******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;
  }
}
import java.util.Scanner;
导入java.util.Random;
公营机库管理
{  
静态扫描仪numIn=新扫描仪(System.in);
静态扫描仪strIn=新扫描仪(System.in);
//用于将用户先前的猜测保留为字符串。
静态StringBuffer=新StringBuffer();
公共静态void main(字符串[]args)
{
字符串类别;
int字长;
内部位置;
int字母保留;
整合式=10;
布尔是猜测词;
猜字母串;
字符串字;
字符串displayWord=“”;
猜字符;
category=getCategory();
word=getWord(类别);
//获取单词的长度。
wordLength=word.length();
LettersMaining=字长;
System.out.println(“单词的长度是:“+wordLength+”characters”);
//生成与word长度相同的“*”并将其存储在“displayWord”中。
for(int i=0;i0&&总计>0)
{
//提示用户猜测字母。
System.out.println(“猜一个字母(注:有“+字长+”字母)”);
guess=strIn.findWithinHorizon(“.”,0).charAt(0);
//检查是否在“word”中猜到了字母。
isGuessInWord=(word.indexOf(guess))!=-1;
//检查猜测是否在“word”中。
if(isGuessInWord==false)
{
总数--;
System.out.println(“对不起,但是单词中没有“+guess+””);
如果(总计<1)
{
System.out.println(“看起来你已经没有生命了!:(”);
}
否则如果(总=1)
{
System.out.println(“小心!你只剩下1条生命了!”);
}
其他的
{
System.out.println(“你还有”+(总数)+“剩余生命!”);
}
} 
其他的
{
System.out.println(“不错!单词中有字母“+guess+””);
用于(位置=0;位置<字长;位置++)
{
字符串newDisplayWord=“”;
用于(位置=0;位置<字长;位置++)
{
if(字字符(位置)=猜测)
{
/*displayWord.charAt(位置).equals(word.charAt(位置))*/
系统输出打印(猜测);
字母保持--;
} 
其他的
{
系统输出打印(“*”);
}
} 
}
}
//保存用户先前猜测的字母以及单词中仍然未知的字母数。
System.out.println();
prevGuessedLetters=buffer.append(猜测);
System.out.println(“先前猜测的字母:+(prevGuessedLetters));
System.out.println(“剩余字母:+(字母保留));
System.out.println(“”);
//检查胜负条件。
如果(字母保留==0)
{
System.out.println(“恭喜,”+word+“'是正确的答案!”);
}
如果(总计<1)
{
System.out.println(“对不起,你输了!”);
System.out.println(“正确答案是“+word+””);
{
打破
}
}
}
}
公共静态字符串getCategory()
{
字符串类别;
System.out.println(“======================================================================”);
System.out.println(“欢迎来到刽子手游戏!”);
System.out.println(“======================================================================”);
while(true)
{
System.out.println(“从以下类别中选择:”);
System.out.println(“1.汽车品牌”);
System.out.println(“2.国家”);
System.out.println(“3.动物”);
System.out.println(“4.Fruit”);
System.out.println(“”);
类别=strIn.nextLine();
if(category.toLowerCase().equals(“1”)| |(category.toLowerCase().equals(“一”))
{
类别=“汽车品牌”;
}
else if(category.toLowerCase().equals(“2”)| |(category.toLowerCase().equals(“2”))
{
类别=“国家”;
}
else if(category.toLowerCase().equals(“3”)| |(category.toLowerCase().equals(“三”))
{
类别=“动物”;
}
else if(category.toLowerCase().equals(“4”)| |(category.toLowerCase().equals(“四”))
{
类别=“水果”;
}
如果(category.toLowerCase().equals(“汽车品牌”)| | category.toLowerCase().equals(“国家”)| | category.toLowerCase().equals(“动物”)| | category.toLowerCase().equals(“水果”))
{
System.out.println(“”);
System.out.println(“不错的选择!您选择了“+category+”category!”);
System.out.println(“”);
打破
}
其他的
{
System.out.println(“对不起,”+category+“'不是有效的输入。请重试!”);
System.out.println(“”);
}
}
退货类别;
}
公共静态字符串getWord(字符串类别)
{
字符串[]卡布兰宝剑={“丰田”、“法拉利”、“本田”、“现代”、“兰博基尼”、“道奇”、“福特”、“雪佛兰”、“菲亚特”、“雷克萨斯”、“大众”、“讴歌”、“奥迪”、“宾利”、“布加迪”、“别克”、“凯迪拉克”};
字符串[]CountrieSwarm={“加拿大”、“英国”、“法国”、“瑞士”、“澳大利亚”、“瑞典”、“希腊”、“意大利”、“墨西哥”、“巴西”、“i”
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;
  }
}