//do-while 循环示例
class dw {
 public static void main(String args[])
throws java.io.IOException {
char ch;
do {
System.out.print("Press a key followed the key of ENTER: ");
//读入一个字符
ch = (char) System.in.read();
   } while(ch!='p');  }}
输出:D:\code>java dw
Press a key followed the key of ENTER: a
Press a key followed the key of ENTER: Press a key followed the key of ENTER: Pr
ess a key followed the key of ENTER: p
请问键入a后怎么会有怎么多"Press a key followed the key of Enter:"
我怎么感觉输出一个"Press a key followed the key of Enter:"
呢?请高手解惑!

解决方案 »

  1.   


    class dw {
    public static void main(String args[]) throws java.io.IOException {
    char ch;
    byte[] buffer = new byte[1024];//定义一个字节数组,用来存储输入的字符后的换行和回车等字符
    do {
    System.out.print("Press a key followed the key of ENTER: ");
    //读入一个字符
    ch = (char) System.in.read();
    System.in.read(buffer);//读取字符后,读取换行和回车等字符
    } while(ch!='p');  }
    }
      

  2.   

    你参考下这个 http://jun0325.javaeye.com/blog/708001
     
    你的System.in.read()忘记考虑 回车符了~
    回车在 控制台中是两个字符\r\n
    所以你应该在
    ch = (char)System.in.read();
    后面再加上过滤回车的 代码ch = (char)System.in.read();
    System.in.read();
    System.in.read();
      

  3.   

    byte[]=new byte[1024]这种想法非常好!
    太有才了,感谢