Answer the question
In order to leave comments, you need to log in
Explain why it works this way?
public class javart {
public static void main(String[]args)
throws java.io.IOException {
char ch, ignore, answer = 'S';
do {
System.out.println("Зaдyмaнa буква из диапазона A-Z.");
System.out.print("Пoпытaйтecь ее угадать: ");
// Получить символ с клавиатуры
ch = (char) System.in.read();
// Отбросить все остальные символы во входном буфере
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
if(ch == answer) System.out.println("** Правильно! **");
else {
System.out.print(" ... Извинитe, нужная буква находится ");
if(ch < answer)
System.out.println("ближe к концу алфавита");
else System.out.println("ближe к началу алфавита");
System.out.println("Пoвтopитe попытку!\n");}
}while(answer != ch);
}
}
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
Answer the question
In order to leave comments, you need to log in
It looks like this was done to remove possible duplicates. Press the keyboard button and hold, you will see that not one letter will be printed, but a series. And this whole series will fly away to System.in.
So it turns out that you either need to skip everything that comes after the first letter, or there will be several messages in a row on the screen that the attempt to guess failed. Those. Here, as it were, the road is being cleared for the next attempt.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question