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 3 of 3

Thread: Hangman game loop

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hangman game loop

    Hello all,
    For my exam we're supposed to be able to write a program that allows for the user to input a phrase, prints that phrase as all "?"s, and then allows for more input as to guess at the phrase and slowly replace the "?"s with the actual letters...
    I have the most of the code done, but I'm having trouble replacing the "?"s with the actual letters, I'll post the code up here..
    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;
     
     
     
     
                   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++)
                	  {
                		  System.out.println("poop");
                		  if(guesschar == phrase.charAt(numloop))
                		  {
                			  guesschar = hiddenphrase.charAt(numloop);
                			  System.out.println(hiddenphrase);
                		  }
     
                	  }
     
                 }
     
                  System.out.println("Congratulations! You win!");
                  System.exit(0);
           }
    }

    How do I set the charAt(numloop) in hiddenphrase to change to guesschar? (in the nested while)
    Thanks!

    EDIT: Ignore the print "poop", I just use that to make sure the loop is working correctly.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Hangman game loop

    Since this is for an exam, should you be getting help from a programming forum?

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hangman game loop

    This program I'm writing is not graded -- on thursday we have to go in and write something similar to this, this program I'm writing is for the study guide.

Similar Threads

  1. For Loop and String Problem - Console Hangman Game
    By ashboi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2012, 12:15 AM
  2. Complete hangman game: Good example for seeing how everything goes together
    By ShadeDarkan in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: July 9th, 2012, 06:31 AM
  3. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  4. Hangman game HELP!
    By KingFisher in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 9th, 2010, 04:23 AM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM