문제

I remember reading somewhere in Java, possibly Oracle documentation that there is a shortcut to assign a flip value of a primitive.

Similar to:

int i = 0;
i += 3;
System.out.println(i);

Output is 3, but what if I wanted -3? Or if given -3, make it positive 3? Rather than doing

i = -i;

Isn't there a shortcut to do it in just the assignment operator?

도움이 되었습니까?

해결책 2

If you want to reverse the sign of a number you can do one of the following things:

i *= -1;
i = -i;

다른 팁

You have lots of options:

i = -3;
i += -3;
i -= 3;

Or even

i = 3;
System.out.println("-" + i);

int i = -3; is your easiest solution

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top