1,105,312 Community Members

Serious issue with Hangman java program

Member Avatar
RaJsHaH.23
Newbie Poster
1 post since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
[import java.util.Scanner;
import java.util.Random;

public class HangmanMy {
  public static void main (String[] args){
    System.out.println("Welcome to the game Hangman :)/>");
    Scanner input = new Scanner(System.in);
    boolean done = false;
    String guess;
    String guesses = "";
    char letter;
    int wrongGuess = 0;
    StringBuffer dashes;
    System.out.println("Choose a category -> places / games / animals / subjects ");
    String words = WordsToChoose(input.nextLine());
    dashes = makeDashes(words);
    while (! done)
    {
      System.out.println("Here is your word: " + dashes);
      System.out.println("Guesses so far: " + guesses);
      System.out.print("enter a guess (letter or word): ");
      guess = input.next().replaceAll("\\W", "");
      // process the guess

      if (guess.length() > 1)
      {
        if (guess.equalsIgnoreCase(words))
          System.out.println("you win!");
        else 
          System.out.println("Thats the wrong guess");
        done=true;
      }
      else // process single letter guess
      {
        letter = guess.charAt(0);
        guesses += letter;
        if (words.indexOf(letter) < 0)  // not there
        {
          wrongGuess++;
          if(wrongGuess < 6)
            System.out.print("Bad guess: ");
          else if(wrongGuess >= 6)System.out.println("Too many Guesses,The real word was: " + words);
        }
        else // letter is in the words
        {
          // put it in dashes where it belongs
          matchLetter(words, dashes, letter); 
        }
        if(words.equalsIgnoreCase(dashes.toString())){ 
          System.out.print("\nyou win!");
          done = false;
        }
      }
    }
  } 
  public static void matchLetter(String words, StringBuffer dashes, char letter)
  {
    for (int index = 0; index < words.length(); index++)
      if (words.charAt(index) == letter)
      dashes.setCharAt(index, letter);
          words = words.replaceAll("\\W","");
    System.out.print("Good guess: ");
  }
  public static StringBuffer makeDashes(String s)
  {
    StringBuffer dashes = new StringBuffer(s.length());
    for (int count=0; count < s.length(); count++)
      dashes.append('-');
    return dashes;
  }

  public static String WordsToChoose(String category)
  {
    Random generator = new Random();
    String name = "";
    if (category.equalsIgnoreCase("places"))
    {
      String places[] = {"Barc elona", "Lond on", "Par is", "Los Angeles", "Miami Beach", "San Diego", "Sydney", "Puerto Rico"};
      name = places[generator.nextInt(8)];
    }
    else if (category.equalsIgnoreCase("games"))
    {
      String games[] = {"Call of duty", "Halo", "FIFA", "Gta", "AssassinsCreed", "Need for speed", "AngryBirds", "FruitNinja"};
      name = games[generator.nextInt(8)];
    }
    else if (category.equalsIgnoreCase("animals"))
    {
      String animals[] = {"eagle", "tiger", "lion", "Jaguar", "rhinoceros", "sharks", "dolphin", "whale"};
      name = animals[generator.nextInt(8)];
    }
    else if (category.equalsIgnoreCase("subjects"))
    {
      String subjects[] = {"physics", "Computer Science", "chemistry", "gym", "english", "religion", "computer tech", "accounting"};
      name = subjects[generator.nextInt(8)];
    }
    return name.toLowerCase();
  }  
} 




//MY PROGRAM DOSEN'T END, IT DOSENT ASK THE USER IF THEY WANT TO PLAY IT AGAIN, ALSO THE DASHES FOR THE WORDS IS MESSED SINCE IT CREATES A DASH FOR SPACE, I NEED UR HELP FAST. AND ONE MOREE THING IF THERE IS MORE THAN 6 WRONG GUESSES THE OUTPUT OF THE REAL WORD IS ON THE TOP INSTEAD OF THE BOTTOMM
//HELP ME!! PLEASE
Member Avatar
jackmaverick1
Posting Whiz in Training
228 posts since May 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 12 [?]
Skill Endorsements: 0 [?]
 
-1
 

Lines 27 to 31 have a problem involving the if and else clauses. I'll leave it to you to find out just what is wrong, becuase this seems to be an assignment. This isn't the only problem I found, just one of the most glaring. Also, in the future, please make your questions lower case and outside of the code block. And just because you say "HELP ME!! PLEASE" does not mean I (or anybody else) will want to help you more.

Hope that helps,
Jack

Member Avatar
corby
Junior Poster
128 posts since Feb 2010
Reputation Points: -4 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
-1
 

"MY PROGRAM DOSEN'T END, IT DOSENT ASK THE USER IF THEY WANT TO PLAY IT AGAIN"
you have a while loop based upon the condition of the "done" variable, more specifcally you hvae while(!done), think about when this case evaluates to false and causes your program to exit the loop and continue with the program execution

Question Answered as of 1 Year Ago by corby and jackmaverick1
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: