package test;
/**从键盘输入数据,当数据为一行时,输出,并输入over结束程序*/
import java.io.IOException;
import java.io.InputStream;
public class SystemInDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
SystemIn.In();
}
}
class SystemIn
{
public static void In() throws IOException {
InputStream inputstream=System.in;
StringBuilder stringbuilder=new StringBuilder();
while(true)
{
int ch=inputstream.read();
if(ch=='\r')
continue;
if(ch=='\n'){
String str=stringbuilder.toString();
if(str.equals("over"))
break;
System.out.println(str);
stringbuilder.delete(0, stringbuilder.length());
} else
stringbuilder.append((char)ch);
}
}
}
依然超级赛亚人 发表于 2014-8-23 08:12
1.read是阻塞式方法,而且读取操作在while(true)循环中,这个循环条件为true,所以没有人为加结束标记的话 ...
﹑淼 发表于 2014-8-23 09:20
1、因为你并没有跳出循环,while的判断条件为true。2、'\r'和'\n'在判断时会自动的转换成对应的ASCII值,再 ...
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |