0

For some reason I cannot get it so when I put in a incorrect answer it only prints one 'wrong' message instead of three. I checked other threads but found none. I would appreciate a solution thanks.

public class GuessLetter {
public static void main(String args[])
throws java.io.IOException {
    char ch, answer = 'k';
    int loop = -1;

    System.out.println("I'm thinking of a letter between A and Z.");
    System.out.println("Can you guess it: ");

    while (loop < 0) {
        ch = (char) System.in.read();

        if (ch == answer) {
            loop++;
            System.out.println("**CORRECT**");
        } else
            System.out.println("Wrong");
    }
}

}

  • Not sure what you're trying to ask (as it's written the code only prints "wrong" once, and it doesn't really imply that it's supposed to print thrice), could you please clarify? – Jeeter Jul 20 '16 at 15:48
  • After pressing the key, are you pressing another key like `` for example? – Peter Lawrey Jul 20 '16 at 15:49
  • 2
    You get wrong three times because you read CR LF. That are the three char you get by System.in.read() add System.in.readLine() after reading the character – Jens Jul 20 '16 at 15:49
  • I would look into Scanner – Romain Hippeau Jul 20 '16 at 15:50
  • 1
    The `if`'s _then_ clause has braces but the `else` clause doesn't. My eyes are in pain... – Idan Arye Jul 20 '16 at 15:52

0 Answers0