Question:
Need help with my java code (2 player hangman)?
Aadil
2013-04-09 13:33:49 UTC
I'm a beginner at java and I was making a simple hangman code, but something's not right. I think I messed up on the loop part.
If I guess a letter correctly, it repeats the code "you got the _th letter right" over and over again.
However, if I guess it a wrong letter, there is an error and the run stops.
How would I let the guesser to keep guessing until 10 wrong guesses? Also, if you have any tips or suggestions, that would be very helpful, as I am learning by myself without a teacher. Thank You!

import java.util.Scanner;

public class TwoPlayerHangman {



public static void main(String[] args) {

Scanner scan = new Scanner (System.in);
Scanner scan1 = new Scanner (System.in);
Scanner scan2 = new Scanner (System.in);

System.out.println("how many letters are in the word? (not more than 6)");
int numberofletters = scan1.nextInt();

System.out.println("write the word");
char [] letter;
letter = new char [numberofletters];

if (numberofletters == 2) {
letter[0] = scan.findInLine(".").charAt(0);
letter[1] = scan.findInLine(".").charAt(0);
}
else if (numberofletters == 3) {
letter[0] = scan.findInLine(".").charAt(0);
letter[1] = scan.findInLine(".").charAt(0);
letter[2] = scan.findInLine(".").charAt(0);
}
else if (numberofletters == 4) {
letter[0] = scan.findInLine(".").charAt(0);
letter[1] = scan.findInLine(".").charAt(0);
letter[2] = scan.findInLine(".").charAt(0);
letter[3] = scan.findInLine(".").charAt(0);
}
else if (numberofletters == 5) {
letter[0] = scan.findInLine(".").charAt(0);
letter[1] = scan.findInLine(".").charAt(0);
letter[2] = scan.findInLine(".").charAt(0);
letter[3] = scan.findInLine(".").charAt(0);
letter[4] = scan.findInLine(".").charAt(0);
}
else {
letter[0] = scan.findInLine(".").charAt(0);
letter[1] = scan.findInLine(".").charAt(0);
letter[2] = scan.findInLine(".").charAt(0);
letter[3] = scan.findInLine(".").charAt(0);
letter[4] = scan.findInLine(".").charAt(0);
letter[5] = scan.findInLine(".").charAt(0);
}

System.out.println("Guesser, guess some letters! (10 wrong guesses then you lose)");

int numberofwrong = 0;
char guess = scan2.findInLine(".").charAt(0);


while (numberofwrong < 10) {

if (guess == letter[0]){
System.out.println("you got the 1st letter right! it is " + letter[0]);
}
else if (guess == letter[1]){
System.out.println("you got the 2nd letter right! it is " + letter[1]);
}
else if (guess == letter[2]){
System.out.println("you got the 3rd letter right! it is " + letter[2]);
}
else if (guess == letter[3]){
System.out.println("you got the 4th letter right! it is " + letter[3]);
}
else if (guess == letter[4]){
System.out.println("you got the 5th letter right! it is " + letter[4]);
}
else if (guess == letter[5]){
System.out.println("you got the 6th letter right! it is " + letter[5]);
}
else if (guess != letter[1] && guess != letter[2] && guess != letter[3] && guess != letter[4] && guess != letter[5] && guess != letter[6]) {
System.out.println("Oops thats not a correct letter" + numberofwrong++);
}
}


}

}
Four answers:
?
2013-04-10 19:36:45 UTC
I couldn't figure out what you were trying to do. Try this instead:



import java.util.Scanner;



public class TwoPlayerHangman {



private static int MAX_LENGTH = 6;

private static int MAX_INCORRECT_GUESSES = 10;

private static int SCREEN_LENGTH = 60;



public static void main(String[] args) {



Scanner scan = new Scanner(System.in);



System.out.println("Enter the word to be guessed (max of " +

MAX_LENGTH + " letters): ");

String wordToGuess = scan.next();



while (wordToGuess.length() == 0 || wordToGuess.length() > MAX_LENGTH) {

System.out.println("Incorrect Size! Enter the word to be guessed (max of " +

MAX_LENGTH + " letters): ");

wordToGuess = scan.next();

}



int numberGuessedWrong = 0;

int numberGuessedCorrectly = 0;

char[] revealedLetters = new char[wordToGuess.length()];

for (int rli = 0; rli
revealedLetters[rli] = '-';

}



for (int rli = 0; rli
System.out.println(" ");

}



System.out.println("Guesser, guess some letters! (" + MAX_INCORRECT_GUESSES +

" wrong guesses then you lose)");

while (numberGuessedWrong < MAX_INCORRECT_GUESSES &&

numberGuessedCorrectly < wordToGuess.length()) {

System.out.println("Enter a character: ");

String guessedCharacter = scan.next();



while (guessedCharacter.length() == 0 || guessedCharacter.length() > 1) {

System.out.println("Only one character, please. Enter a character: ");

guessedCharacter = scan.next();

}



int matchedCharAt = wordToGuess.indexOf(guessedCharacter);

if (matchedCharAt != -1) {

revealedLetters[matchedCharAt] = guessedCharacter.charAt(0);

numberGuessedCorrectly++;

while (matchedCharAt != -1) {

matchedCharAt = wordToGuess.indexOf(guessedCharacter, matchedCharAt+1);

if (matchedCharAt != -1) {

revealedLetters[matchedCharAt] = guessedCharacter.charAt(0);

numberGuessedCorrectly++;

}

}

System.out.println("Good guess!: " + new String(revealedLetters));

} else {

numberGuessedWrong++;

System.out.println("Oops! " + numberGuessedWrong + " of " +

MAX_INCORRECT_GUESSES + " wrong guesses: " + new String(revealedLetters));

}

}



if (numberGuessedCorrectly == wordToGuess.length()) {

System.out.println("Congratulations! You got: " +

wordToGuess);

} else {

System.out.println("Better luck next time. The word was: " +

wordToGuess);

}

}

}
anonymous
2016-10-07 08:00:17 UTC
2 Player Hangman
MaryAnn
2016-01-28 07:16:21 UTC
java code 2 player hangman
Tiertza
2015-08-20 18:24:54 UTC
This Site Might Help You.



RE:

Need help with my java code (2 player hangman)?

I'm a beginner at java and I was making a simple hangman code, but something's not right. I think I messed up on the loop part.

If I guess a letter correctly, it repeats the code "you got the _th letter right" over and over again.

However, if I guess it a wrong letter, there...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...