下面是我编写的一段程序,在屏幕中输入字符或者字符串,当输入“bye”时,程序就停止请大家帮我看下,当我输入“bye"时程序也未停止,是何原因?我用的是equals方法,应该没有错的import java.io.IOException;
public class ReadLine {

/**
 * Method main
 *
 *
 * @param args
 * @throws IOException 
 *
 */
public static void main(String[] args) throws IOException {
// TODO: Add your code here
byte [] buf=new byte[1024];
String strInfo=null;
int pos = 0;
int ch=0;
System.out.println("Plese entre info:");
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.equals("bye"))
{
return;
}
else
{
System.out.println(strInfo);
pos = 0;
break;
}
default:
buf[pos]= (byte)ch;
}
}

}
}

解决方案 »

  1.   

    你明摆着要去读入字符串的。为什么不用:
    BufferedReader buf1 = new BufferedReader(new InputStreamReader(System.in));
      

  2.   

    根据楼上的稍微改了一下
    import java.io.*;public class Test {public static void main(String[] args) throws IOException {
    // TODO: Add your code here
       try{
            InputStreamReader keyboard =new InputStreamReader(System.in);
            BufferedReader ins = new BufferedReader(keyboard);        System.out.println("Plese entre info:");
            while(true){
                String str=ins.readLine();
                if (str.toLowerCase().equals("bye"))
                    return;
                else
                    continue;
            }
       }catch(Exception e){
           e.printStackTrace();
        
        }
           
       }    
    }
        
      

  3.   

    String strInfo=“”;