Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hangman program

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman program

    Hi all, I am taking a very beginner java class at my high school, and am completely stuck on a program we are working on. It is essentially a very simple hangman game, in which the user enters a 5 letter word, then enters guesses. I will post the directions as well as what i have.
    Thanks,
    Perry



    Write a hangman game. Your game should:
    * Ask the user for a 5 letter lower case word. Error check the
    * length of the word to make sure it's 5 letters long. You
    * may assume all of the letters are lower case.
    * Print out guess (it should start as 5 underlines) and ask the
    * user for a lower case letter. This should all be in ONE
    * JOP window.
    * Check to see if the letter is in the word and update the guess
    * if it is. You do not need to show this in a JOP window.
    * Continue displaying the guess and asking for letters until
    * the user has guessed the word.
    * Print a congratulations message that contains the word and
    * the number of letters the user had to enter.
    *
    * Use the main method below. DO NOT change it. You can
    * comment out method calls until you write them.
    *
    * Things you could add:
    * Have it work for any sized word.
    * Have it work for upper & lower case letters.
    * Show the incorrect letters the user has chosen.


    ----------------------code(Java)
    public class Hangman
    {
    	public static void changeJOP()
    	{
    		UIManager.put("Label.font", new FontUIResource
    				(new Font("Nyala", Font.BOLD, 28)));
    		UIManager.put("OptionPane.messageForeground",Color.white);
    		UIManager.put("Panel.background",new Color (0,0,205));
    		UIManager.put("OptionPane.background",Color.white);
    		UIManager.put("Button.background",Color.white);
    		UIManager.put("Button.foreground", new Color(25,25,112));
    		UIManager.put("Button.font", new FontUIResource
    				(new Font("Nyala", Font.BOLD, 14)));
    	}
    	public static String enterWord(String word)
    	{
    		int num;
    		String s; 
    		do{
    		s = (JOptionPane.showInputDialog("Enter a 5 letter word:"));
    		num= s.length();
    		}while(num != 5);
    		return s;
     
    	}
    	public static void enterLetter(char letter, String word)
    	{
     
    	}
    	public static void checkLetter()
    	{
     
    	}
     
     
    	public static void main(String[] args)
    	{
    		changeJOP();
    		String word="", guess="     ";
    		char letter;
    		int count = 0;
    		word= enterWord();
    		while(!guess.equals(word))
    		{
    			letter = enterLetter(guess);
    			count++;
    			guess=checkWord(word, guess, letter);
    		}
    		printCongrats(word, count);
    	}
    }
    ---------------------------------
    The main was built for us, everything else i did.
    I have been looking at it all day, and cannot come up with anything to do next.
    Thanks all for your time..
    Last edited by pbrockway2; January 5th, 2013 at 12:54 AM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: Hangman program

    Hi psgtyler, welcome to the forums!

    I have added "code" tags to your post (so I can read it The idea is you put [code] at the start of a section of code and [/code] at the end. That way the forum software displays it with indentation intact. The same applies to error messages and things where indentation is necessary for readability - there seems to be no "pre" markup, so we use [code]...[/code] there too. It doesn't matter with this code, but you might find using *spaces* rather than tabs to indent is a good idea. Tabs can be a bit unpredictable at the best of times, and many browsers render them uncomfortably wide.

    [Edit]

    Take it a step at a time. Document your code: that is describe in the code exactly what enterWord() is supposed to do, then test to make sure it really does that. If you get a compiler message you can't understand, post it and I'm sure someone will explain what it means.

    Also don't be in a hurry to race into the code. Think about *what* each of those four methods are supposed to do. It might help to think about how you (rather than a computer) would carry out the procedure described in the instructions with another person playing the part of the user.

Similar Threads

  1. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  2. Hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 10:07 PM
  3. Need help with my hangman program!
    By mbae in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 29th, 2012, 07:24 PM
  4. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  5. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 07:04 AM

Tags for this Thread