Буферизация консольного ввода (Код из Шилда)
Добавлено: 12 май 2015, 19:23
Здравствуйте.
Учу Java 7 по Шилду. Руководство для начинающих.
3 глава, 105 стр.
Вопрос по этому коду:
[syntax=java]// Guess the letter game, 4th version.
class Guess4 {
public static void main(String args[])
throws java.io.IOException {
char ch, ignore, answer = 'K';
do {
System.out.println("I'm thinking of a letter between A and Z.");
System.out.print("Can you guess it: ");
// read a character
ch = (char) System.in.read();
// discard any other characters in the input buffer
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
if(ch == answer) System.out.println("** Right **");
else {
System.out.print("...Sorry, you're ");
if(ch < answer) System.out.println("too low");
else System.out.println("too high");
System.out.println("Try again!\n");
}
} while(answer != ch);
}
}[/syntax]
Не смотря на описание не пойму как это работает:
[syntax=java]
// discard any other characters in the input buffer
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
[/syntax]
В приложении объяснение по книге:
Учу Java 7 по Шилду. Руководство для начинающих.
3 глава, 105 стр.
Вопрос по этому коду:
[syntax=java]// Guess the letter game, 4th version.
class Guess4 {
public static void main(String args[])
throws java.io.IOException {
char ch, ignore, answer = 'K';
do {
System.out.println("I'm thinking of a letter between A and Z.");
System.out.print("Can you guess it: ");
// read a character
ch = (char) System.in.read();
// discard any other characters in the input buffer
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
if(ch == answer) System.out.println("** Right **");
else {
System.out.print("...Sorry, you're ");
if(ch < answer) System.out.println("too low");
else System.out.println("too high");
System.out.println("Try again!\n");
}
} while(answer != ch);
}
}[/syntax]
Не смотря на описание не пойму как это работает:
[syntax=java]
// discard any other characters in the input buffer
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
[/syntax]
В приложении объяснение по книге: