我正在閱讀一本關於java書的入門書,並且在本節中涉及while while循環。這裏是代碼:丟棄輸入緩衝區中的字符?
public class GuessingGameV4 {
public static void main(String[] args) throws java.io.IOException {
char ch, ignore, answer;
answer = 'K';
do {
System.out.println("I am thinking of a letter between A and Z");
System.out.println("Can you guess it?");
ch = (char) System.in.read();
do {
ignore = (char) System.in.read();
} while (ignore != '\n');
if (ch == answer) System.out.println("Right!!! ");
else {
System.out.print("Sorry you are ");
if (ch < answer) System.out.print("too low.\n");
else System.out.print("too high.\n");
System.out.println("Try again\n");
}
} while (answer != ch);
}
}
內部do while循環涉及忽略是放棄輸入緩衝區中的字符的代碼的一部分。
我只是想更多地說明它實際上做了什麼,因爲書籍的解釋並不適合我。