我如何利用一元增量&减少这样做 [英] How do I utilize unary increment & decrement to do this

查看:66
本文介绍了我如何利用一元增量&减少这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了问题而且我不确定实际上我在哪里出错了。



我附上了Hastebin链接( hastebin [ ^ ]),对于我遇到的问题。它还解释了老师要我们做什么。



我显然没有得到这个,我愿意打赌它很简单,我只是俯视它,但我确实需要一些帮助。



请帮我理解发生了什么。



我尝试了什么:



我已将我写的代码附加到我在问题中添加的链接中。我不知道该去哪里完成任务我觉得我在其他地方读过的教程没有帮助我,因为我没有从这里开始去哪里

So I'm having an issue and I'm not sure where I'm going wrong actually.

I've attached the Hastebin link (hastebin[^]), to the issue that I'm having. It also explains what the teacher wants us to do.

I'm obviously not getting this and I'm willing to bet it's something very simple and I'm just overlooking it, but I really do need some help.

Please help me understand what's going on.

What I have tried:

I've attached the code that I've written to the link that I put in my problem. I'm not sure where to go in order to complete the task and I guess that the tutorials I've read elsewhere aren't helping me because I'm not getting where to go from here

推荐答案

因此,如果增加一个字节将从127变为-127。变量包含其最大值和最小值。此外,如果你做x ++,它返回x,然后加1。所以你需要++ x,将1加到x然后输出它。



So, a byte will go from 127 to -127 if you increment it. Variables wrap around their max and min values. Also, if you do x++, it returns x, then adds 1 to it. So you need ++x, to add 1 to x and THEN output it.

126
127
-127
-127
127
127
-127





这就是你想要的。





That's what you want.

byte x = 126;





这就是你开始的地方。所以:





That's where you start. So:

        System.out.println(x); // 126
System.out.println(++x); //127
x++;
System.out.println(++x); //-127
System.out.println(x); // -127
x--;
System.out.println(--x); //127
System.out.println(x); // 127
++x;
System.out.println(++x); //-127





我已经运行了它并且有效



I've run this and it works


其实我是思考基督徒也是如此。但是当您在repl.it运行此代码时



Actually, I was thinking along those lines as well Christian. However when you run this code at repl.it

class Main {
  public static void main(String[] args) {
   byte x = 126;

System.out.println(x); // 126
System.out.println(x++); //127
System.out.println(x++); //-127
System.out.println(x); // -127
System.out.println(x--); //127
System.out.println(x); // 127
System.out.println(x++); //-127

  }
}





输出126,126,127,-128,-128,127,127



It outputs this 126, 126, 127, -128, -128, 127, 127


这篇关于我如何利用一元增量&减少这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆