下面是我编写的一段程序,在屏幕中输入字符或者字符串,当输入“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;
}
}
}
}
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;
}
}
}
}
BufferedReader buf1 = new BufferedReader(new InputStreamReader(System.in));
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();
}
}
}
。