publicclassExchange{publicstaticvoidmain(String[] args){// 定义第一个变量int first =5;// 定义第二个变量int second =6;// 定义第三个变量,并使其等于第一个变量int third = first;// 交换变量值
first = second;
second = third;System.out.println("first的值为:"+first+",second的值为:"+second);}}
方法二
publicclassExchange{publicstaticvoidmain(String[] args){// 定义第一个变量int first =5;// 定义第二个变量int second =6;
first = first + second;
second = first - second;
first = first - second;System.out.println("first的值为:"+first+",second的值为:"+second);}}
方法三
publicclassExchange{publicstaticvoidmain(String[] args){// 定义第一个变量int first =5;// 定义第二个变量int second =6;
first = first ^ second;
second = first ^ second;
first = first ^ second;System.out.println("first的值为:"+first+",second的值为:"+second);}}