java中三个加号(减号)的程序阅读题,你能做对吗?

一、
int m1=10,m2=10,m3=8;
m3 *= m1+++m2;  
System.out.println("m3:" + m3);

二、
int t = testM(10);
System.out.println("testM:" + t);

private int testM(int m) {
  //System.out.println("testm:" + m);
  if(1 > m)
   return 0;
  else
   return m---testM(m);// 如果改成m-(--testM(m))呢
 }

请大家想下运行的过程以及可能的结果


一、m3:160
解析:1)单目运算符优先级高于双目运算符
      2)++运算符从左到右结合
      m3 *= (m1++)+m2;//运算后m1的值是11了
      m3 *= 20;
      m3 = 8*20 = 160
错误理解:
1)m3 *= m1+(++m2);// 这样算出168
2) (m3*=m1++)+m2;//错误The left-hand side of an assignment must be a variable
3)(m3*=m1)+++m2;//错误1Invalid argument to operation ++/--,错误2Syntax error on token "+", invalid AssignmentOperator

二、testM:5
解析:原理和第一题一样
(m--)-testM(m);
testM(10) = 10 - testM(9) = 5
testM(9) = 9 - testM(8) = 5
testM(8) = 8 - testM(7) = 4
testM(7) = 7 - testM(6) = 4
testM(6) = 6 - testM(5) = 3
testM(5) = 5 - testM(4) = 3
testM(4) = 4 - testM(3) = 2
testM(3) = 3 - testM(2) = 2
testM(2) = 2 - testM(1) = 1
testM(1) = 1 - testM(0) = 1
错误理解:
1) m-(--testM(m))// Invalid argument to operation ++/--
2) m-testM(m);//testM(10) = 10 - testM(10) 栈溢出:java.lang.StackOverflowError

你可能感兴趣的:(Java开发,找工作)