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.