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',为什么?
解决方案 »
- nextInt*()函数源代码的解释 求助
- 请问Struts2核心拦截器的这种功能是怎么实现的?
- 数据结构的意义
- 怎么快速读取这钟字符串一次两个"str1/str2/str3.."
- 还是这个保存到Word的问题,没有解决啊
- 我在用socket对数据时,出现invalid stream header的错误,请问这是什么原因呢?
- java中如何用系统默认的程序打开某个文件?简单问题高分相送...解决后另送100。
- jbuilder7.0中,如何调试apleet?
- 打包后,为何图片路径为空。谢谢。
- JAVA SOCKET 多客户端 长连接
- 谁知道用jFreeChart生成两张图表,并且在同一界面上显示他们?
- 一个小问题?求助!
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
不知道是不是?
* @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
--
对