Aadil
2013-04-09 13:33:49 UTC
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++);
}
}
}
}