Thread: Hangman project; replace the secret word with underscores

Results 1 to 4 of 4
  1. #1 Hangman project; replace the secret word with underscores 
    Donator

    Nintendo's Avatar
    Join Date
    Oct 2008
    Posts
    1,681
    Thanks given
    195
    Thanks received
    184
    Rep Power
    231
    Code:
    String secretWord = "Nintendo";
    Scanner input = new Scanner(system.in);
    
    System.out.print("Guess a letter: ");
    String letter = input.nextLine();
    char guessedLetter = letter.charAt(0);
    
    
    if (secretWord.indexOf(guessedLetter) != -1) {
        System.out.println("You got it right!");
    } else {
        System.out.println("Wrong!");
    }
    }
    Code:
    I'm trying to print out 8 underscores so like
    System.out.println("_ _ _ _ _ _ _ _"); since "Nintendo" contains 8 letters.
    
    and if you guess a "N" it then prints out (" N _ n _ _ n _ _ ") for example...
    Any ideas?
    Reply With Quote  
     

  2. #2  
    Registered Member Tx-Sec's Avatar
    Join Date
    Jan 2008
    Age
    27
    Posts
    521
    Thanks given
    34
    Thanks received
    15
    Rep Power
    68
    You need to make a string array with the lenght of the word and fill it with _ and once it guess a letter you replace the array index with the word index using substring method.
    I made a shitty Hangman game yesterday. if you want, you can inbox me and i can give you the code
    Reply With Quote  
     

  3. #3  
    Banned

    Join Date
    Aug 2012
    Posts
    611
    Thanks given
    88
    Thanks received
    118
    Rep Power
    0
    char[] mask = new char[secretWord.length()];
    Arrays.fill(mask, "_");

    That answer should suffice if you're just creating the mask. Once you create the mask, then everything else should be easy because you're just replacing the index of the array with the correctly guessed letter.
    Reply With Quote  
     

  4. Thankful user:


  5. #4  
    Registered Member
    CTucker's Avatar
    Join Date
    Oct 2008
    Posts
    2,422
    Thanks given
    263
    Thanks received
    281
    Rep Power
    343
    Quote Originally Posted by Wildskiller View Post
    char[] mask = new char[secretWord.length()];
    Arrays.fill(mask, "_");

    That answer should suffice if you're just creating the mask. Once you create the mask, then everything else should be easy because you're just replacing the index of the array with the correctly guessed letter.
    Don't forget to make changes to the mask and recreate the string when you make a correct guess.
    Reply With Quote  
     

  6. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 6
    Last Post: 10-05-2011, 12:26 AM
  2. Replies: 13
    Last Post: 04-11-2008, 10:33 PM
  3. beta project-rs the sound server beta
    By laurens in forum RS2 Server
    Replies: 18
    Last Post: 09-29-2007, 06:59 PM
  4. Project Scape v3.5 Client with custom items/377
    By blackfirexx in forum RS2 Client
    Replies: 11
    Last Post: 09-07-2007, 12:48 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
  •