这些数是怎么计算的?Java codeint a 3a + a - a * aSystem.out.println(a)// 输出-3int b 10b
这些数是怎么计算的?
Java code
int a = 3; a += a -= a * a; System.out.println(a); // 输出-3 int b = 10; b = b++; System.out.println(b); // 输出10 int c = 5; c += c++ + (c++ + 0); System.out.println(c); // 输出16 int d = 3; d = (d++) + (d++) + (d++); System.out.println(d); // 输出12
[解决办法] 这些是程序语言基础的运算符优先级问题,请楼主学习一下+-*/和++等的优先级,就明白了 [解决办法] 都是从右到左 int a = 3; a += a -= a * a; ---------------------- 1:a*a=6 2:a -= a*a就等于a = a- a*a;所以a=-6 3:a += (**);就等于a = a+(**) 4:所以a += -6=>a=a-6=3 =================================== int b = 10; b = b++; 这个在Java中,是b先赋值后自加,所以左边的b还是10 打印b是打印左边的b,C++就是打印右边的b ========================================= int c = 5; c += c++ + (c++ + 0); 这个不是很懂啊 它等于int b = c+ c++ +(c++ +0)
[解决办法] 一题
Java code
int a = 3;a += a -= a * a;//先计算a*a=9;-->再计算3-9=-6-->再计算a=3+(-6)=-3System.out.println(a); [解决办法]
Java code
int a = 3; a += a -= a * a;// 先算 a*a; 然后 a += a; 最后(a+=a) -= (a*a) System.out.println(a); // 输出-3 int b = 10; b = b++; System.out.println(b); // 输出10 // 等价 b = b; System.out.println(b); b++; int c = 5; c += c++ + (c++ + 0); System.out.println(c); // 输出16 //先 (C++ + 0),就这个c=6,其他两个都是5 int d = 3; d = (d++) + (d++) + (d++); //d = 3 + 4 + 5;; d++先使用d,在使d增加1 System.out.println(d); // 输出12 [解决办法]
------解决方案--------------------
研究后我这里说下我对b++的理解
b++ 可以看成一个函数 {temp = b,b += 1, return temp}
也就是说 b = 10, c = b++ 的话, 最后 b=11, c=10
可是 b = b++ , b++ 之后 b=11,但是返回值是temp,也就是10,这个时候等于 b = temp 也就是 b =10 [解决办法] 如果我是领导,有谁写出这样的代码,我会立马让他回家!