黑马程序员技术交流社区

标题: 求运行过程 [打印本页]

作者: 杨胜男    时间: 2014-6-6 11:58
标题: 求运行过程
class  Test2
{
        public static void main(String[] args)
        {
                int x=0,y=1;
                if(++x==y--&x++==1||--y==0)
                        System.out.println("x="+x+",y="+y);
                else
                        System.out.println("y    ="+y+",x="+x);
        }
}

//if语句如何执行的,执行结果如何?

作者: shisishishisi    时间: 2014-6-6 12:21
本帖最后由 shisishishisi 于 2014-6-6 12:47 编辑
  1. [code]class  Test2
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int x=0,y=1;
  6.                 if(++x==y--&x++==1||--y==0)
  7.                         //1==1 & 1 == 1 || 0 ==0  x=2,y=0
  8.                         // true & true || true
  9.                         //true || true
  10.                         //true
  11.                         System.out.println("x="+x+",y="+y);//x=2,y=0
  12.                 else
  13.                         System.out.println("y    ="+y+",x="+x);
  14.                         
  15.         }
  16. }
复制代码
[/code]
作者: 姿_`态    时间: 2014-6-6 12:40
class  Test2
{
        public static void main(String[] args)
        {
                int x=0,y=1;
                if(++x==y--&x++==1||--y==0)                /*


                        ++x表示先自增,然后x=1,y--先使用y的值y=1 然后y自减,所以第一个判断++x==y--是true,然后
                          ,x++表示先使用x值,然后自增,所以x=2  所以第二个判断是 true,此时y=1,最后一个判断式--y==0,先是y自减,然后判断,结果true
                        所以 true&true||true,其实后面的--y== 0,不判断也可以了 ,因为||  逻辑运算符,左侧true 右侧不用判断, 所 以此时有结果了,x=2,y=0
                */
                        System.out.println("x="+x+",y="+y);
                else
                        System.out.println("y    ="+y+",x="+x);
                        
        }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2