Java_定义两个变量a和b,不使用第三个变量,使两个值交换

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/Chill_Lyn/article/details/102640315
public class Test{
	public static void main(String[] args) {
		swap(5,6);
	}
	
	public static void swap(int a,int b) {
		System.out.println(a+" "+b);
		a+=b;
		b=a-b;
		a=a-b;
		System.out.println(a+" "+b);
	}
}

结果
5 6
6 5

猜你喜欢

转载自blog.csdn.net/Chill_Lyn/article/details/102640315