按下Enter键会显示一行你刚输入的字符,我把'\r'注释掉,再执行程序时,如果我输入是end,并没有返回到OS,程序如下,我知道在windows中按下Enter后就等于同时按下了'\r'和'\n',请问回车与换行有啥区别呢,在程序中一定要分开对待吗?程序功能:输入一个字符串,按下Enter后回显在屏幕上,再输入下一个字符串,直到输入end为止。
class test{
public static void main(String[] args){
int index=0;
byte[] buffer=new byte[1000];
String str=null;
int ch=0;
System.out.println("Please input some string:");
while(true){
try{
ch=System.in.read();
}catch(Exception e){
e.printStackTrace();
}
switch(ch){
case '\r': break; //回车
case '\n': //换行
str=new String(buffer,0,pos);
if(str.equalsIgnoreCase("end")) return;
else{
System.out.println(str);
index=0;
break;
}
default:
buffer[index++]=(byte)ch;
}
}
}
}
class test{
public static void main(String[] args){
int index=0;
byte[] buffer=new byte[1000];
String str=null;
int ch=0;
System.out.println("Please input some string:");
while(true){
try{
ch=System.in.read();
}catch(Exception e){
e.printStackTrace();
}
switch(ch){
case '\r': break; //回车
case '\n': //换行
str=new String(buffer,0,pos);
if(str.equalsIgnoreCase("end")) return;
else{
System.out.println(str);
index=0;
break;
}
default:
buffer[index++]=(byte)ch;
}
}
}
}
CSDN小助手 是一款脱离浏览器也可以使用csdn论坛的
软件!
界面: http://qqwwee.com/
下载: http://qqwwee.com/csdn.rar 包含源代码