The user enters a code, then it gets hidden. For example, if the phrase entered it "java rules", the computer displays it as "??? ???". Then, the user inputs a letter. If the letter is right, say "a", then it should display, "?a?a ???".
So far, after one letter is entered, it prints each letter of the secret phrase twice (so it prints "???a???a ???"). After two letters are entered, it prints each letter of the secret phrase three times(I guessed "r" and then got ???a???a? ???r???").
I think I know what I'm doing wrong, but I have no idea how to fix it! Also, I can't figure out how to make the program terminate when the secret phrase has been completly revealed. Any help would be greatly appreciated! I'm still pretty knew to this, so please explain your thoughts.
Side note: I can't use anything I haven't learned, which basically means I'm limited to loops and if statements.
Here's the code so far:
If I input the secret phrase to be "one two", then it outputs it as "??? ???", which is correct. However, then I'm supposed to guess the letters, so I input the letter "o". Instead of displaying "o?? ??o" like it should, it displays "?o??? ???o". It's printing each letter in the secret phrase twice. I know its doing this because the length of the guesses String is two, so it's going through the loop twice. I just have no idea how to rearrange my code so this doesn't happen.
I suggest starting with moving the code that gets the first guess to BEFORE the loop that compares guess(es) to the phrase. It will go within the outermost loop, but outside the two others, since you want the guessing code to repeat.
Then I would alter my code so that, after each guess, for each letter in the phrase:
if the letter was in the string keeping correctly guessed letters, output that letter,
else if the letter was the one guessed, output THAT letter and add it to the string of correctly guessed letters,
else output a '?'
to terminate the program; perhaps create a boolean variable called, say, "done". Instead of the outermost loop, you can use a while loop:
if you're allowed to use those. Just before checking a new guess, set it to true; if you output a ?, indicating there are still unguessed letters, set it to false.
If you're not allowed to use while loops yet, then if you get through all the letters and done is still true, put in the line "System.exit();"
Let us know how it goes!
I still have a problem running the loop, though I get your logic. I tried to add the changes, but it still doesn't work right.
Here's the new code: (I added the section in the beginning to display the hidden phrase before the user guesses - that's part of the needed program).
What is going wrong in the 3rd loop part?
First of all -- when you put code on JavaRanch, please enclose it in code tags. That means put openbracket, then "code", then closebracket, then all your code, then openBracket, slash, "code", and closebracket. That formats it to be easier to read on the website.
Second of all -- the reason your code doesn't enter your loop is the construct "if (done = false)" - in Java, the single equals sign is assignment, never equality testing, and what you meant was "done == false. (Incidentally, if someone says you should use "(!done)" instead, they aren't really wrong -- both these things return true if the boolean value is false, and yours is easier for a beginner to read.)
So -- the next problem is that you're altering "correctLetters" inside a loop that is looping on the length of correctLetters; that's generally a bad idea. And using the logic the way I laid it out is going to make that difficult to change that way. So let me alter a simpler alternative.
Keep a list of guessed letters instead of a list of correct letters. No matter what the user enters, add it to your string. We should change the name of it now; it is a string of guessed letters. Then your logic can go through all your guessed letters to see if they are in the string when you're doing your output.
You'll also need to know whether you output any letter in that loop; I've put in another boolean to be set true if I find and output a guessedLetter as part of the phrase. If I didn't, then I need to output a question mark.
I hesitate to do this, but it's late and I'm heading to bed after this post; because of that, and because I led you wrong, I'm just going to put my solution in here. I suggest you use it to alter your program instead of just copying this one, but no one can say you have not made an effort on the problem.
Once you are happy with this, I have a challenge for you -- extra credit, so to speak -- there is a method on String named "indexOf()". Look at it and see if you can figure out how it might be used to simplify your program a bit.
Since she is new, I’ll add the code tags, and we can see how much better it looks . . . if you don’t add your own line numbers.
Ralph Cook wrote: . . . please enclose it in code tags. . . .
I've read through your code several times now and have a couple quick questions: What is the significance of break at line 47? Wouldn't it work just as well without it? What exactly does it do here?
Thanks so much again!
I have not thought through what happens if your user guesses the same letter twice -- I suspect that, with the break, the code is all right; without it, it might print the letter twice?
Nicole Miller wrote:I've read through your code several times now and have a couple quick questions: What is the significance of break at line 47? Wouldn't it work just as well without it? What exactly does it do here?
A tip from an old hand: when you're trying to solve a problem in Java, very often the best way to do it, oddly enough, is to avoid Java altogether.
Right now, you're worrying about the bits and bytes, and what a break means, and when to set variables, and when to exit a loop....DON'T.
Turn off your computer and sit down with a piece of paper and pencil, and concentrate on the problem.
Pick a phrase and go through the business of selecting a letter and filling it in. What's happening? What steps are you going through to fill the letter in in the correct places? Describe them in English (or your native language), and don't stop until you are absolutely sure of all the steps required to do the task.
THEN turn your computer back on and start writing Java. You'll find the whole process a lot easier when you understand the problem first.