需求:通过键盘录入数据,当录入一行数据后,就将该行数据进行打印,如果录入的数据是over,那么停止录入。
/*System.out:对应的是标准输出设备,控制台
* System.in:对应的是标准输入设备,键盘
*/
public class ReadIn {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
while(true) {
int ch = in.read();
if(ch=='\r') {
continue;
}
if(ch=='\n') {
String s = sb.toString();
if("over".equals(s)) {
break;
}
System.out.println(s.toUpperCase());
sb.delete(0, sb.length());
}else {
sb.append((char)ch);
}
}
}
}
该程序演示了如何从键盘读取用户输入的数据,逐字符处理,直到遇到'over'为止。当输入一行非'over'的数据时,程序将其转换为大写并打印,然后清空缓冲区,等待下一行输入。

435

被折叠的 条评论
为什么被折叠?



