这个题有很多解法,以下方案只是最容易想到的。
结果:
a = 13
b = 21
public class SwitchWithoutNewVariables {
/**
* @param args
*/
public void shift(){
int a = 21;
int b = 13;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a);
System.out.println("b = " + b);
}
public static void main(String[] args) {
SwitchWithoutNewVariables m = new SwitchWithoutNewVariables();
m.shift();
}
结果:
a = 13
b = 21