基础问题,编译器通不过,谁来注解下Java codepublic class ReadLine{public static void main(String[] arg
基础问题,编译器通不过,谁来注解下
- Java code
public class ReadLine{ public static void main(String[] args) { byte [] buf=new byte[1024]; String strInfo=null; int pos=0; int ch=0; while(true) { try { ch=System.in.read(); } catch(Exception e) { e.printStackTrace(); } switch(ch) { case'\r': break; case'\n': strInfo=new String(buf,0,pos);//什么意思? if(strInfo=='bye') { return; } else { System.out.println(strInfo); pos=0; break; } default: buf[pos++]=(byte)ch; } } }}我的编释器通不过,请问是哪里出得错?
顺便请注释下while里面的语句含义。
深表感谢!全分
[解决办法]
编译错误是:strInfo=='bye'
strInfo是字符串类型,应用用双引号,不是单引号
[解决办法]
- Java code
public class ReadLine{ public static void main(String[] args) { byte [] buf=new byte[1024]; String strInfo=null; int pos=0; int ch=0; while(true) { try { ch=System.in.read(); } catch(Exception e) { e.printStackTrace(); } switch(ch) { case'\r': break; case'\n': strInfo=new String(buf,0,pos);//取buf,0到pos出来形成字符串 if(strInfo=='bye')//字符是一个一个的,能这么写吗?改成"bye".equals(strInfo) { return; } else { System.out.println(strInfo); pos=0; break; } default: buf[pos++]=(byte)ch; } } }}
[解决办法]
if(strInfo=='bye')
字符串必须要用equals()方法判断,而且字符串是""的 