现在的位置: 首页 > 综合 > 正文

不定义中间变量的情况下,实现两个数交换数值

2013年12月10日 ⁄ 综合 ⁄ 共 325字 ⁄ 字号 评论关闭
public class ChangeTwoNumber{
	public static void main(String[] args){
		changeTwoNumber(1,2);
	}
	
	/**
	 * 交换两个int数的值
	 */
	static void changeTwoNumber(int a, int b){
		System.out.println("what user input is: a = " + a + ", b = " + b);
		a = a ^ b;
		b = a ^ b;
		a = a ^ b;
		System.out.println("what the program output is: a = " + a + ", b = " + b);
	}
}

执行的结果:

what user input is : a = 1, b = 2

what the program output is: a = 2, b = 1

抱歉!评论已关闭.