Guess the letter

The first time the program waits until I enter the symbol, and the second time with the variable res - no. Instead of waiting for keyboard input, the program immediately enters the loop. What is the problem?

public class Alphavite { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Загадайте букву"); char ch = (char) reader.read(); System.out.println("Угадайте букву"); char res = (char) reader.read(); while ((ch >= 'A' && ch <= 'Z') || (ch >= 'a') && (ch <= 'z')) { if (res == ch) { System.out.println("Вы угадали! Загаданная буква - " + ch); break; } else { System.out.println("Не угадали. Попробуйте снова"); break; } } } 

}

  • Make a conclusion entered after each input, it will immediately become clear where to dig. - Andrey Shpileva
  • By the way, if you enter 2 characters at once, everything will work - Ziens

1 answer 1

 public class Alphavite { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Загадайте букву"); char ch = (char) reader.read(); reader.readLine();// ?! System.out.println("Угадайте букву"); char res = (char) reader.read(); while ((ch >= 'A' && ch <= 'Z') || (ch >= 'a') && (ch <= 'z')) { if (res == ch) { System.out.println("Вы угадали! Загаданная буква - " + ch); break; } else { System.out.println("Не угадали. Попробуйте снова"); break; } } } } 

Or like this:

 public class Alphavite { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Загадайте букву"); char ch = reader.readLine().charAt(0); System.out.println("Угадайте букву"); char res = reader.readLine().charAt(0); while ((ch >= 'A' && ch <= 'Z') || (ch >= 'a') && (ch <= 'z')) { if (res == ch) { System.out.println("Вы угадали! Загаданная буква - " + ch); break; } else { System.out.println("Не угадали. Попробуйте снова"); break; } } } } 

But I do not quite understand why there is a cycle.

  • 2
    Here it is necessary to clarify that in console java-applications there is no possibility to read individual keystrokes of the keyboard. Entry always ends with ENTER. See also (1) , (2) - Astronavigator
  • Yes, thanks for the addition - Ziens
  • "Here it is necessary to clarify that in console java-applications there is no possibility to read individual keystrokes of the keyboard. Input is always completed by pressing ENTER." Or write your Sensor using while and Thread.sleep and do certain actions on a specific XA character! - Denis Kotlyarov
  • @DenisKotlyarov, in this case it was enough just to use another method. Why reinvent the wheel? HA! - Ziens
  • "Why reinvent the bike" 1.Velo which provides java only works on Enter, + drags behind it a bunch of unnecessary in the RAM. 2. opportunity without translation to String (XA!), 3.You can do anything and anything on any character (XA!). And so this advice is that nothing is impossible (and not to the topic :)) Ha! (how I love this XA! :)) - Denis Kotlyarov