+ 1
Swap 2 variables' value
//Please explain why this statement doesn't work in Java: //a -= b = (a += b) -b; //Thanks public static void main(String[] args) { int a = 5, b = 7; System.out.printf("a = %d, b = %d\n",a,b); //Swap a and b without using another variable //Work in C and Java a += b; b = a - b; a -= b; //But when I use this statement: // a -= b = (a += b) -b; //Only work in C and not in Java System.out.printf("a = %d, b = %d\n",a,b); }
2 Réponses
+ 1
@Shikamaru: your statement is easer to understand and works in both C and Java. Nice.
But I also want to know the problem in the statement:
a -= b = (a += b) -b;