为了账号安全,请及时绑定邮箱和手机立即绑定

赋值运算为何是这样的

 three=one+two;
          System.out.println(three);
          three+=one;
          System.out.println(three);
          three-=one;
          System.out.println(three);
          three*=one;
          System.out.println(three);
          three/=one;
          System.out.println(three);
          three%=one;
          System.out.println(three);

   谁能解释下为何代码是这样的,这个代码看不懂为何three=one+two;却输出System.out.println(three);的结果是30而不是0

正在回答

代码语言

4 回答

这题首先是已经将局部变量three初始化为0,它是一个局部变量,然后局部变量变成了one+two,此时three的值已经为30,然后输出它的值为30,后面做的three+=one;这时three的值已经为30,然后加上one,变为40,总而言之,three 局部变量的值会随之改变。

0 回复 有任何疑惑可以回复我~

代码不完整,one,two,three.是如何定义的呢?


0 回复 有任何疑惑可以回复我~

three是左值,“=”是赋值符号:将右边运算的值赋值给左边的值(左值)。左值必须是变量,不能是常量,比如3=1+2是不合法的,计算机认为是错误的,编译不能通过。

0 回复 有任何疑惑可以回复我~

three=20+10=30;   three是one与two的和。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

赋值运算为何是这样的

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号