java - How to replace a string with dashes or question marks? -
so i'm in process of making hangman game user enters in word , word displayed disguised in question marks. every time user enters in word correctly, letter appears in place of question mark. have code necessary guessing , etc, need someones disguising word entered in user in question marks.
public static void main(string[] args) { scanner bob = new scanner(system.in); arraylist<string> alphabet = new arraylist<string>(arrays.aslist("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"));//i looked part mr. a, didn't know how directly set values in arraylist int guesses = 6;//wrong guesses system.out.println("hello! please enter in word game. 1 word allowed!"); string word = bob.next(); int lettersinword = word.length();//so know if you've solved word or not while(guesses > 0 && lettersinword > 0){ //not or because you'll end getting negative 1 system.out.println("what guess? 1 letter @ time please or you'll mess game."); string guess = bob.next(); guess.tolowercase(); //i populated arraylist lowercase letters has way while(!alphabet.contains(guess)){ //since remove letters arraylist, default make sure guess doesn't happen twice system.out.println("you guessed letter, or not letter"); guess = bob.next();//just lets user make guess } alphabet.remove(guess);//takes letter out of alphabet no redoes happen if(word.contains(guess)){ system.out.println("yeauhhhhh, thats right! guessed " + guess); lettersinword--;//makes word size smaller } else { system.out.println("nahhhhh, that's wrong! guessed " + guess + " , not in word"); guesses--;//takes guess away } system.out.println("you have " + guesses + " wrong guesses left"); //displays @ end of iteration } if(guesses == 0){//this comes when lose system.out.println("you have lost, word "+ word); } else {//this comes when you've guessed word, if not 1 other!!! system.out.println("you have won! word "+ word + "good job dude. please see\n" + "mr. free piece of cake , 100 on final."); } }
you create separate string of question marks same length word.
string marks = ""; (int = 0; < word.length(); i++) { marks += "?"; }
when letter guessed correctly, replace question mark letter.
Comments
Post a Comment