Define integer variables a and b, and write a program to exchange the values of the two variables a and b (the third variable cannot be introduced)

# Define integer variables a and b, and write a program to exchange the values ​​of the two variables a and b (the third variable cannot be introduced)



import java.util.Scanner;
//定义整型变量 a、b,写出将 a、b 两个变量值进行互换的程序(不能引入第三变量)
public class Demo06 {
	public static void main(String[] args) {
		Scanner scan =new Scanner(System.in);
		System.out.println("请输入整数a:");
		int a=scan.nextInt();
		System.out.println("请输入整数b:");
		int b=scan.nextInt();
		System.out.println("交换后:");
		 change(a,b);	
	}
	public static void change(int a,int b)
	{
		a = a+b;
		b =a-b;
		a =a-b;
		System.out.print("a="+a+" ");
		
		System.out.print("b="+b);
		
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324103174&siteId=291194637