Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Hangman Game loop

  1. #1
    Grocerybag is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Hangman Game loop

    Hey guys,
    I'm working on a homework assignment where we have to create a program that allows the user to input a string, then re-print that string as all "?"s, and then allow for more input to guess at what the string is and replace each "?" when you guess correctly. Right now I'm stuck on replacing the "?"s with the character guess, so any help is appreciated!
    Java Code:
    import java.util.Scanner;
    public class Assn07
    {
    
           public static void main(String[] args)
           {
                   Scanner stdIn = new Scanner(System.in);
                   
                   String phrase;
                   String hiddenphrase;
                   int numchars;
                   int numloop;
                   String guess;
                   hiddenphrase = "";
                   char guesschar;
                   char hidden;
                   
                   
                   
                   
                   System.out.println("Enter the common phrase to be guessed:");
                   phrase = stdIn.nextLine();
                   
                   
                   numchars = phrase.length();
                   numloop = 0;
                   System.out.println("\n");
                   
                   
                   for(numloop = 0;numchars > numloop; numloop++)
                   {
                	   char qmarks = phrase.charAt(numloop);
                	   
                	   if(qmarks == ' ')
                	   {
                		  hiddenphrase += " ";
                	   }
                	   else
                	   {
                		   hiddenphrase += "?";
                	   }
                	   
                   }
                   System.out.println(hiddenphrase);
                   
                   
                   
                  while(hiddenphrase != phrase)
                  {
                	  System.out.print("Enter your character guess!");
                	  guess =stdIn.next();
                	  guesschar = guess.charAt(0);
                	  for(numloop = 0; numchars > numloop; numloop++)
                	  {
                		  hidden = hiddenphrase.charAt(numloop);
                		  System.out.println(guesschar);
                		  
                  		  if(guesschar == phrase.charAt(numloop))
                		  {
                			  hiddenphrase.replace(hidden,guesschar);
                			  System.out.println(hiddenphrase);
                			  System.out.println(hidden);
                		  }
                		  
                	  }
                		  
                 }
                       
                  System.out.println("Congratulations! You win!");
                  System.exit(0);
           }
    }
    Last edited by Grocerybag; 11-07-2012 at 03:35 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Grocerybag is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Hangman Game loop

    The two websites are different, I'm just asking for as much help as I can!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Hangman Game loop

    Quote Originally Posted by Grocerybag View Post
    The two websites are different, I'm just asking for as much help as I can!
    Yes they're different, but that doesn't mean that you should simply post everywhere without notifying us. We're all volunteers, and don't appreciate wasting our time and efforts answering questions that may have been answered elsewhere just because a poster didn't have respect for our free time.
    DarrylBurke likes this.

Similar Threads

  1. Hangman Game
    By getshum in forum New To Java
    Replies: 1
    Last Post: 02-26-2011, 05:01 AM
  2. Hangman Game Help Please
    By 9tjh in forum New To Java
    Replies: 4
    Last Post: 12-04-2009, 04:19 AM
  3. Need Some Help, Simple Loop For Hangman Game
    By Juo in forum New To Java
    Replies: 2
    Last Post: 11-14-2009, 08:51 PM
  4. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 07:47 PM
  5. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 02:56 PM

Posting Permissions

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