java用尽可能多的方式实现两个整数交换

/**
 * 
 */
package com.gem.demo.day03_practice;

import java.util.Scanner;

/**
 *
 * Description:1.用尽可能多的方式实现两个整数交换
 *
 * @author HadwinLing
 *
 * @date 2020年1月13日上午8:39:39
 *
 * @version 0.0.1 
 *
 */
public class practice01_01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入a的值");
		int a = input.nextInt();
		System.out.println("请输入b的值");
		int b = input.nextInt();
		//方法一
//		int temp =a;
//		a= b;
//		b = temp;	
		//方法二
//		a = (a+b)-(b=a); 
		//方法三
		a = a^b^(b=a);
		System.out.println("a ="+a+" ,b="+b);
	}
}

发布了42 篇原创文章 · 获赞 12 · 访问量 6127

猜你喜欢

转载自blog.csdn.net/Alingyuzi/article/details/103952231