package cn.tedu.demo;
/**
* @author 赵瑞鑫 E-mail:[email protected]
* @version 1.0
* @创建时间:2020年7月16日 下午5:32:28
* @类说明: 两个数交换的三种解决方案
*/
public class Demo13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//两个数交换的三种解决方案
//添加一个中间变量
int x = 1, y = 2;
int z;
z = x;x = y;y = z;
System.out.println(x+","+y);
//异或
x = 1; y = 2;
x = x ^ y;
System.out.println(x);
y = x ^ y;
x = x ^ y;
System.out.println(x+","+y);
//加减
x = 1;y = 2;
x = x + y;
y = x - y;
x = x - y;
System.out.println(x+","+y);
}
}