Wednesday, 15 May 2013

Hangman Part 2 with Arrays Java -



Hangman Part 2 with Arrays Java -

well in class assigned hangman project utilize of arrays , have generate " _ "'s. have generated blanks when user inputs "wordanswer". problem cant figure out how replace right guess char blank " _ ".

here code whole programme , know not complete.

import cs1.keyboard; public class game { public static void main(string[] args) { int correctguess = 0; int wrongguess = 0; int bodyparts = 0; char guess; boolean foundit; system.out.println("welcome hangman!"); system.out.println("please come in word other user guess!"); string wordanswer = keyboard.readstring(); char[] chars = wordanswer.tochararray(); char[] blanks = new char[wordanswer.length()]; system.out.println("you have 6 attempts guess word!"); { (int = 0; < wordanswer.length(); i++) { system.out.print(" _ "); } system.out.println("please come in in guess!"); guess = keyboard.readchar(); (int = 0; < chars.length; i++) { if (guess == chars[i]) { blanks[i] = guess; foundit = true; } } } while (correctguess < wordanswer.length() && wrongguess != 6); { system.out.println("congratulations! have solved word!"); } } }

i know have utilize loop able alter blank right guessed letter when run programme , come in in right guess blank not alter right letter. here output

welcome hangman! please come in word other user guess! programme have 6 attempts guess word! _ _ _ _ _ _ _ please come in in guess! p _ _ _ _ _ _ _ please come in in guess! r _ _ _ _ _ _ _ please come in in guess! o _ _ _ _ _ _ _ please come in in guess! g _ _ _ _ _ _ _ please come in in guess! r _ _ _ _ _ _ _ please come in in guess! _ _ _ _ _ _ _ please come in in guess! m _ _ _ _ _ _ _ please come in in guess!

what expecting is!!

welcome hangman! please come in word other user guess! programme have 6 attempts guess word! _ _ _ _ _ _ _ please come in in guess! p p _ _ _ _ _ _ please come in in guess! r p r _ _ _ _ _ please come in in guess! o p r o _ _ _ _ please come in in guess!

etc...

this have tips , help appreciative!!

system.out.println("please come in in guess!"); guess = keyboard.readchar(); (int = 0; < chars.length; i++) { if (guess == chars[i]) { blanks[i] = guess; foundit = true; } }

after line

char[] blanks = new char[wordanswer.length()];

add following:

for(int = 0; < wordanswer.length(); i++) { blanks[i] = '_'; }

then, instead of system.out.print(" _ "), system.out.print(" " + blanks[i] + " ");

just know, need prepare comes after do ... while loop. incorrectly tell person have solved word, if unsuccessful. don't need { ... } after while. also, foundit variable isn't doing yet, although need handle wrong guesses 1 time point..

java arrays loops for-loop

No comments:

Post a Comment