Results 1 to 3 of 3
  1. #1
    Juo
    Juo is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default Need Some Help, Simple Loop For Hangman Game

    Hey guys,

    So im trying to program a very simple version of Hangman, I plan on making it a bit more complicated than this but so far ive got a bit stuck.

    Java Code:
    import javax.swing.*;
    
    import java.lang.String;
    
    public class Hangman {
    	
    	public static void main(String[] args)
    	{
    		int count = 0;
    		String word = "bureau";
    		String[] user={"-","-","-","-","-","-"};
    		char guess;
    		
    		
    		do {
    			String input = JOptionPane.showInputDialog        
    			("Please enter a character, you current progress is " + user[0] + user[1] + user[2] + user[3] + user[4] + user[5]);
    			guess = input.charAt(0);}
    		
    		while(count < 9); {
    			if(guess == word.charAt(0)); {
    				user[0] = "b";
    				count++;
    			}
    			if(guess == word.charAt(1)); {
    				user[1] = "u";
    				count++;
    			}
    			if(guess == word.charAt(2)); {
    				user[2] = "r";
    				count++;
    			}
    			if(guess == word.charAt(3)); {
    				user[3] = "e";
    				count++;
    			}
    			if(guess == word.charAt(4)); {
    				user[4] = "a";
    				count++;
    			}
    			if(guess == word.charAt(5)); {
    				user[5] = "u";
    				count++;
    			}
    			
    		}
    		
    			JOptionPane.showMessageDialog(null,"Sorry you lose"); 
      }
    }
    This is my code, I know its absolutely hideous but I was wondering if anyone could tell me why it doesnt actually work?

    The problem is that count doesnt seem to be increasing and the String[] user={"-","-","-","-","-","-"}; array doesnt seem to be updating with the "new" letters.

    If anyone could help id really appreciate it.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    First of all you should declare a char[] using single quotes {'-', '-', '-' ...etc} not a string array also your if statements should be inside the while loop, or you should make another method. Also, after while(count < 9); you have an open bracket which doesnt need to be there (and its close bracket). Another thing, after your if (guess == word.charAt(0)) you have a semi-colon, this makes your if statement not do anything.

    Here is the code you should use, examine it and find out where you went wrong:
    Java Code:
       import javax.swing.*;
    
       import java.lang.String;
    
        public class Hangman {
       
           public static void main(String[] args)
          {
             int count = 0;
             String word = "bureau";
             char[] user={'-', '-', '-', '-', '-', '-'};
             char guess;
          
          
             do {
                String input = JOptionPane.showInputDialog        
                   ("Please enter a character, you current progress is " + user[0] + user[1] + user[2] + user[3] + user[4] + user[5]);
                guess = input.charAt(0);
                count++;
                if(guess == word.charAt(0))
                {
                   user[0] = 'b';
                }
                if(guess == word.charAt(1)) 
                {
                   user[1] = 'u';
                }
                if(guess == word.charAt(2)) 
                {
                   user[2] = 'r';
                }
                if(guess == word.charAt(3)) 
                {
                   user[3] = 'e';
                }
                if(guess == word.charAt(4)) 
                {
                   user[4] = 'a';
                }
                if(guess == word.charAt(5)) 
                {
                   user[5] = 'u';
                }
             
             }
             
             while(count < 9); 
             
                     
             
          
             JOptionPane.showMessageDialog(null,"Sorry you lose"); 
          }
       }

  3. #3
    Juo
    Juo is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    wow thanks very much for the fast detailed response

    Here is the code you should use, examine it and find out where you went wrong:
    I will do and also plan to stick around reading the new posts in the New to Java area, I might learn something :)

Similar Threads

  1. [help] game loop
    By Fuzzier in forum Java Applets
    Replies: 3
    Last Post: 10-31-2009, 09:02 AM
  2. Hangman Game..
    By iPetey in forum New To Java
    Replies: 4
    Last Post: 05-07-2009, 03:24 PM
  3. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 07:47 PM
  4. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 02:56 PM
  5. Create the game Hangman
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •