<p>import java.io.IOException; 
import java.io.InputStream;</p><p>public class ReadFromKeyboard { 
public static void main(String[] args) throws IOException { 
  readFromKeyboard(); 
} 
public static void readFromKeyboard() throws IOException 
{ 
  InputStream input = System.in;  
   
  System.out.print("请输入:"); 
   
  StringBuilder str = new StringBuilder();//定义一个字符串缓冲区,用于存储从键盘获取到的字符串 
   
  while(true) 
  { 
   int ch = input.read(); 
   if(ch == '\r') 
    continue; 
   if(ch == '\n') 
   { 
    String s = str.toString//如果没写这一步,你输入的over会追加到原来的字符串中. 
    if("over".equals(str))//键盘录入必须确定结束的条件,这是必须要确定的.这里的结束条件为当输入over就结束. 
     break; 
    System.out.println(str); 
    str.delete(0, str.length()); 
   } 
   else 
    str.append((char)ch); 
  } 
} 
} 
</p> 
 |