Reputation: 31
I'm doing my first programming language, Java, and I am having trouble getting correct guesses to print correctly. When a wrong letter is entered, it prints "wrong!" but when a correct letter is guessed, it prints both "Correct!" and "Wrong!"...any help is appreciated. Thanks. :)
The words are pulled from an array.
import java.util.Scanner;
import java.util.Random;
public static void main(String[] args) {
System.out.println("Welcome to Hangman! Below you can find the rules of the game.");
//Welcome message
System.out.println();
//Spacing
System.out.println("How to play:");
System.out.println();
//Spacing
System.out.println("Hangman is a word guessing game. Guess the word by suggesting letters within a certain number of guesses.");
System.out.println("The word to guess is represented by a row of dashes, representing each letter of the word.");
System.out.println("If you guess a letter correctly, it replaces a dash and you can continue to guess until the word is uncovered.");
System.out.println("However, if you guess wrong you lose a try. You have a maximum of 5 tries. After guessing 10 words, you win! Good luck!");
//Rules of game
System.out.println();
//Spacing
Scanner in = new Scanner (System.in);
//Scanner created
String[] intro = {"after", "think", "could", "thank", "round", "clump", "plane", "beach", "towel", "tiger", "goat", "monkey", "house"};
String[] beginner = {"around", "amount", "grown", "focus", "cedar", "flute", "unicorn", "fruit", "basket", "burger", "chips", "juice"};
String[] intermediate = {"about", "bring", "clean", "mother", "locker", "hunter", "drink", "eight", "spray", "untie", "cents"};
String name;
int age;
//Variables initialized
System.out.println("Please enter your name:");
//Prompt for name
name = in.next();
//Input for name
System.out.println("Please enter your age:");
//Prompt for age
age = in.nextInt();
//Input for age
if (age >= 4 && age < 7){
System.out.println("You have been placed in the Intro level!");
Random rand = new Random();
//Random created
int randomWord = rand.nextInt(12)+0;
//Stores the position of array
String word2guess = intro[randomWord];
//String is the value stored in the generated position of array
char[] word2Char = word2guess.toCharArray();
//Stores the word in a char array
char[] wordLength = new char [word2Char.length];
//Creates an array for word length
for (int i = 0; i < word2Char.length; i++){
wordLength[i] = '_';
}
//Replaces each letter of the word with an underscore
for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
//Prints the word in underscore format with a space between each letter
int wrongGuesses = 0;
int correctGuesses = 0;
boolean letterFound = false;
boolean nextWord = true;
while (nextWord){
while(correctGuesses != word2Char.length){
System.out.println();
//Spacing
System.out.println("Please enter a letter:");
char guess = in.next(".").charAt(0);
//Input for only ONE letter
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false && wrongGuesses <= 4){
wrongGuesses++;
System.out.println("Wrong! You have "+ (5 - wrongGuesses)+" attempts remaining. Please try again.");
}else{
correctGuesses++;
System.out.println("Correct!");
}
for (int i = 0; i < word2Char.length; i++){
System.out.print(wordLength[i] + " ");
}
System.out.println();
//Spacing
if (correctGuesses == word2Char.length){
System.out.println("Congratulations! You guessed the word. The word was: "+word2guess);
nextWord = true;
}
if (wrongGuesses == 5){
System.out.println();
//Spacing
System.out.println("Game Over! You have no attempts remaining. The word was: "+word2guess);
nextWord = false;
break;
}
}
}
Upvotes: 0
Views: 166
Reputation: 1251
This section of code:
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
correctGuesses++;
System.out.println("Correct!");
}
}
if(!letterFound){
wrongGuesses++;
System.out.println("Wrong!");
}
Needs to be this:
for (int i = 0; i < word2Char.length; i++){
if (guess == word2Char[i]){
wordLength[i] = word2Char[i];
letterFound = true;
break;
}else{
letterFound = false;
}
}
if(letterFound == false){
wrongGuesses++;
System.out.println("Wrong!");
}else{
correctGuesses++;
System.out.println("Correct!");
}
The issue was that in the for loop, even if letterFound
was true, the loop would keep on checking each char. It would've only worked if someone guessed the last char correctly. So what I did was add a break
statement so that whenever letterFound
is true, the for loop exits.
Upvotes: 1
Reputation: 2276
Notice that in java, this branch would never be used:
if (solved = false) {
wrongGuesses++;
}
Please change that to
if (solved == false) {
wrongGuesses++;
System.out.println("Wrong!");//And then add the wrong print out here
}
I would like you to take a look here and here too
Upvotes: 1