如题:
运行以下程序
import java.io.IOException;
public class test {
    public static void main(String[] args) throws IOException {
            for(int j = 0; j < 5; j++) { 
               System.out.println("INPUT:"); 
               char c =  (char) System.in.read(); 
               if(c == '1') { 
                 System.out.println("OK!"); 
                } 
               if(c == '\r'){
                System.out.println("\\n"); 
               }
               
             }      }
}
当两次输入1时结果是:
INPUT:
1
OK!
INPUT:
\n
INPUT:
INPUT:
1
OK!
INPUT:
\n
分两次输入2,1时:
INPUT:
2
INPUT:
\n
INPUT:
INPUT:
1
OK!
INPUT:
\n
两次输入11,1时:
INPUT:
11
OK!
INPUT:
OK!
INPUT:
\n
INPUT:
INPUT:
1
OK!
输入111时:
INPUT:
111
OK!
INPUT:
OK!
INPUT:
OK!
INPUT:
\n
INPUT:
麻烦帮我解释一下,谢谢各位了。

解决方案 »

  1.   

    首先程序肯定会打出5个input.我觉得你想自己测试的话加System.out.println("input: " + j);测试
    因为Sytem.in.read()每次只接受一个int字符.
    第一次实际上输入为1,回车键, 1, 回车键
    其他的依次类推
      

  2.   

    哦,我明白了,当我吧程序改成
    import java.io.IOException;
    public class test {
        public static void main(String[] args) throws IOException {
                for(int j = 0; j < 5; j++) { 
                   System.out.println("INPUT:"); 
                   char c =  (char) System.in.read(); 
                   if(c == '1') { 
                     System.out.println("OK!"); 
                    } 
                   if(c == '\r'){
                    System.out.println("\\r"); 
                   }
                   if(c == '\n'){
                    System.out.println("\\n"); 
                   }
                   
                 }      }
    }
    之后,就一目了然了,就是说enter是输入了两个字符 一个是\r 一个是\n,谢谢各位的回答。