public class ReadLine{
public static void main(String[] args){
//byte buf[]=new byte [1024];//
            int buf[]=new int[1024];
String strInfo=null;
int pos=0;
int ch=0;
System.out.println("please enter info, input bye for exit");
while(true){
try{
ch=System.in.read }
catch(Exception e){
System.out.println(e.getMessage());
}
switch(ch){
case '\r':
break;
case '\n':
strInfo=new String(buf,0,pos if(strInfo.equals("bye")){
System.out.println("exit the program");
return;
}
else
System.out.println(strInfo);
pos=0;
break;
default:
buf[pos++]=ch;
}
}


}
}//键入ENTER的时候,按说是break出来,但是好像是执行了case '\n'
键入ENTER的时候,按说是执行case '\r': break;出来,但是好像是执行了case '\n',为什么?

解决方案 »

  1.   

    我写了一个测试ENTER 的ASCII码的程序,
    public class TestEnter { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int i=0;
    System.out.println("Please press enter!");
    try{
    i=System.in.read();}
    catch(Exception e){
    System.out.println(e.getMessage());
    }
    System.out.println("The ASCII of ENTER is:");
    System.out.println(Integer.toHexString(i)); }}结论是ENTER是d,也就是\r
    不知道是不是?
      

  2.   

    好像是说在WINDOWS下,ENTER键是\r\n,那么我的第二个程序就有问题。只是读入了第一个字符\r。
      

  3.   

    public class TestEnter { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    int i=0;
    System.out.println("Please press enter!");
    while(true){
    try{
    i=System.in.read();}
    catch(Exception e){
    System.out.println(e.getMessage());
    }
    System.out.println("The ASCII of ENTER is:");
    System.out.println(Integer.toHexString(i));
    }
    }}
    改了一下程序,发现确实是WINDOWS下ENTER为\r\n
      

  4.   

    WINDOWS下ENTER为\r\n
    --
      

  5.   

    windows下是回车换行   /r/n