java int x,y=5;x=++y;if (x==y) x*=2; if (x>y) x++; else x=y-1

来源:百度知道 编辑:UC知道 时间:2024/05/21 19:57:40
int x,y=5;
x=++y;
if (x==y) x*=2;
if (x>y) x++;
else x=y-1;
我算是7啊,JAVA怎么算是13呢?怎么算的?

int x,y=5;
x=++y; // x==6 ,y==6
if (x==y) x*=2; //条件为ture x==12
if (x>y) x++; //条件为ture 执行x++, 所以你打印X就等于13
else x=y-1;

public class TestMath{

public static void main(String[] args){
int x,y=5;
x=++y;
System.out.println(x);//x=6
System.out.println(y);//y=6
if (x==y) x*=2; //x=12 y=6
if (x>y) x++; //x=13 y=6
else x=y-1;
System.out.println(x);//x=13
System.out.println(y);//y=6
}
}
注意++y 与y++的区别,x=++y 先加1,gmf

int x,y=5;//在这里,x和y的值都是5
x=++y; //这里,首先是++y,把y的值自加1,=6,再赋值给了X.其实在这里,x和y是相等的.都是6!
if (x==y) x*=2; //显然,条件成立,所以,x就=12了...
if (x>y) x++;//这个也成立,所以,X就=13
else x=y-1;

结果出来了..