键盘读取录入当录入的字符为quit时,程序退出 否则 则将所有字符转变成大写输出
1.使用scanner循环录入,判断录入的字符串是否和quit相等,相等则退出,不相等则将字符串大写出
2.自己创建一个控制台的输入流,来录入字符串(模拟Scanner)
// 获取一个存储字符串的容器
StringBuilder sb = new StringBuilder();
// 获取字节输入流 来读取控制台输入的字符串
InputStream in = System.in;
// 一个一个读效率太过于滴 循环读的话又会将回车\r换行\n读取出来 所以就要进行判断
// int ch = in.read();
// System.out.println(ch); //97 a
// System.out.println(in.read()); //98 b
// System.out.println(in.read()); //99 c
// int ch1 = in.read();
// System.out.println(ch1); //100 d
// System.out.println(in.read()); //13 \r
// System.out.println(in.read()); //10 \n
/**
* 循环读取的操作
*/
int ch = 0 ;
while ((ch = in.read()) != -1) {
if (ch == '\r') {
continue;
}
if (ch == '\n') {
// 当读取到\n的时候就代表已经读取完毕了回车换行也读取进去了,也就可以判断字符串是否与quit相等
String result = sb.toString();
if ("quit".equals(result)) {
break;
} else {
// 输出大写字符串
System.out.println(result.toUpperCase());
sb.delete(0, sb.length());
}
} else { // 拼接其他字符串
sb.append((char)ch);
}
}
}