我在使用以下代码时遇到问题:
public class DWDemo {
public static void main(String[] args) throws java.io.IOException {
char ch;
do {
System.out.print("Press a key followed by enter: ");
ch = (char) System.in.read(); // get the char
} while (ch != 'q');
}
}
出于某种原因, System.out 行重复了 3 次。控制台的示例输出如下:
Press a key followed by enter: a
Press a key followed by enter: Press a key followed by enter: Press a key followed by enter:
我已经在 Eclipse Kepler 中尝试过这段代码,并在手动编译时遇到了同样的问题。事实证明,谷歌搜索答案是徒劳的。有什么想法吗?
添加了正确的代码
如果我输入超过 1 个字符,我会得到 4 个 System.out.println 结果:
Press a key followed by enter: aa
Press a key followed by enter: Press a key followed by enter: Press a key followed by enter: Press a key followed by enter: