This week's book giveaways are in the Cloud and AI/ML forums. We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line! See this thread and this one for details.
my program seems to have some difficulties to detect repeating letters, when i input the same letter twice, guessedLetters adds to itself regardless if the letter is already in the word or not, and it stops calling my methods and starts to only run the UsedLetter method after my first guess.
When i comment out the 45th line, everything seems to be in working order, except for the fact that it still doesn't care if i have inputted the same letter twice or not.
Here is my code:
Here is my expected output:
Here is my actual output:
And if i guess a letter wrong, it would minus 1 from the lives and not put anything inside the rigthGuess array but will still insert the user input into the doubleLetter array
I have removed your bold tags because they make the text hard to read.
Don't use two Scanners for System.in. You only ever need one such Scanner.
Why are all your methods marked static? Why have your methods got so many parameters? Those things both suggest you aren't using program state, which means you aren't using object orientation properly.
You can reduce lines 14‑24 to one line.
Although you have indented your code, and used code tags, it is still had to read. I think that is because you have lots of empty lines inside methods and no empty lines between methods. Make sure to separate methods by empty lines and make them so short you don't need empty lines inside them. Also, make each method do one thing. Let it print an array or fill it. Not both.
Where are you keeping a record of wrong letters guessed?
Taurus Ying wrote:my program seems to have some difficulties to detect repeating letters, when i input the same letter twice, guessedLetters adds to itself regardless if the letter is already in the word or not...
Well, quite apart from Campbell's good advice, this program has started out life with an awful lot to do - keep track of letters guessed, "levels", guesses, "lives" - and a lot of it is redundant information.
1. Forget about "levels" for now. They are your "invention" and not part of the game of Hangman (at least as I know it).
Once you get a basic game working, you can always add them in.
2. Keep track of guesses OR lives left. You don't need both. At least not right now.
3. Forget about "duplicate" guesses. If the user is too stupid to understand the game (assuming you've described it properly), that's their lookout and they've lost a life.
4. Start coding your game with a single "word", and 9 lives, and get that working. Every time you run it - with every combination of inputs you can think of, especially invalid ones like duplicate guesses.
THEN you may be ready to start changing words or the number of lives, or adding "levels", etc.
How does Hangman work? What is the purpose of the game? Can you explain it to us in English?
For instance, I would expect to see at least two things after each guess (assuming you're not displaying the "gallows"):
+ The letters guessed so far (including duplicates).
+ Correct letters substituted into the original word.
so, for the word 'HANGMAN', after an initial guess of 'A', I would expect to see something like:
Your guess: A
Result : _A___A_
Letters used: A
Lives left : 9
And after 'N'
Your guess: N
Result : _AN__AN
Letters used: A, N
Lives left : 9 and so on.
Hope it helps.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here