public class test {
public static void main(String[] args) {
byte buf[] = new byte[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':
System.out.println("\\r");
break;
case '\n':
strInfo = new String(buf, 0, pos);
if (strInfo.equals("bye")) {
System.out.println("\\n");
return;
} else
System.out.println(strInfo);
pos = 0;
break;
default:
buf[pos++] = (byte) ch;
}
}
}
}
----------------------------------------
case '\r':
case '\n':有什么区别,不都是按一下回车键吗,请教一下。什么时候执行\r什么时候执行\n,
感谢
public static void main(String[] args) {
byte buf[] = new byte[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':
System.out.println("\\r");
break;
case '\n':
strInfo = new String(buf, 0, pos);
if (strInfo.equals("bye")) {
System.out.println("\\n");
return;
} else
System.out.println(strInfo);
pos = 0;
break;
default:
buf[pos++] = (byte) ch;
}
}
}
}
----------------------------------------
case '\r':
case '\n':有什么区别,不都是按一下回车键吗,请教一下。什么时候执行\r什么时候执行\n,
感谢
感谢什么时候执行\r什么时候执行\n,
感谢
什么时候执行\r什么时候执行\n,
感谢什么时候执行\r什么时候执行\n,
感谢
什么时候执行\r什么时候执行\n,
感谢
windows的换行是回车+换行,即"\r\n"所以,不关怎样, 换行的时候都会执行到 case "\n"