日期:2014-05-20  浏览次数:20682 次

一个小程序结果的费解!
class     Demo
{
    public   static   void   main(String[]   args)   throws   Exception
    {  
        int   a=0;
        w:while(true){
            for(int   i=0;   i <3;   i++){
                int   ch=System.in.read();
                if(ch== 'x ')
                      continue   w;
                else   if(ch== 'y ')
                      break   w;
                else
                      System.out.print((char)ch+ " "+(++a));
            }
            System.out.println( "> ");
        }
        System.out.println();
    }
}
在DOS命令行中输入abcde
结果是
abcde
a1b2c3>
6> e5

7
d为什么没有输入呢?
如果出入abcdef
abcdef
a1b2c3>
d4e5f6>
7
8
哪位达人可以解释一下这个程序读取时的运行流程?
先谢过!



------解决方案--------------------
我晕倒,想了办天没搞明白那W:是啥```看到后面continue w;才知道是跳转```
不要用跳转啊,我打从学开始就没用过,虽然我也不是什么高手```
------解决方案--------------------
其实就是输出的时候把 "回车 "ch=13, "换行 "ch=10输出了。然后把行首的字符给弄乱了。
你可以把print换成println试试。你会发现其实输出d4了。我加了一个判断,当遇到13,10时不输出
class Demo{
public static void main(String[] args) throws Exception{
int a=0;
w:while(true){
for(int i=0; i <3; i++){
int ch=System.in.read();
if(ch== 'x ')
continue w;
else
if(ch== 'y ')
break w;
else{
if((ch!=13)&&(ch!=10))
System.out.println((char)ch+ " "+(++a));
}
}
System.out.println( "> ");
}
System.out.println();
}
}