读入键盘输入,当输入bye时,退出。
注意:①对于回车换行,在windows下面,有'\r'和'\n'两个,而unix下面只有'\n',但是写程序的时候都要把他区分开。
②对于System.in.read函数,每次读入的是一个bye,返回的是int,接收时候用byte和int都可以,但是用char什么的不行。
下面是具体代码:
package com.banyDuncan.huichehuanhang;import java.io.IOException;public class myBye {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint ch = 0;// 用于接收一个System.in.read();int[] temp = new int;int pos = 0;String str = "";// 用于判断是不是ByeSystem.out.println("Input,you the Fucker!");while (true) {try {ch = System.in.read();} catch (IOException e) {e.printStackTrace();}switch (ch) {case '\r':break;case '\n':str = new String(temp, 0, pos);if (str.equalsIgnoreCase("BYE")) {return;}else {System.out.println(str);pos = 0;break;}default:temp = ch;pos++;}}}}
运行结果:
Input,you the Fucker!abcabchaohaobye
页:
[1]