java中将输入的字符打印出来,当输入bye退出程序

代码如下:

public class ReadLine {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        byte[] buf = new byte[1024];
        String strInfo = null;
        int pos = 0;
        int ch = 0;
        System.out.println("Please enter 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.equalsIgnoreCase("bye")) // equalsIgnoreCase函数忽略大小写
                {
                    return;
                } else {
                    System.out.println(strInfo);
                    pos = 0;
                    break;
                }
            default:
                buf[pos++] = (byte) ch;
            }
        }
    }
}

运行结果:
java中将输入的字符打印出来,当输入bye退出程序_第1张图片

你可能感兴趣的:(java)