首页 > 代码库 > java-不用辅助变量,两变量直接交换

java-不用辅助变量,两变量直接交换

 1 import java.util.Scanner;
 2 
 3 public class VariableExchange {
 4     public static void main(String[] args) {
 5         Scanner input=new Scanner(System.in);
 6         System.out.println("please enter a number");
 7         int num1=input.nextInt();
 8         System.out.println("please enter other number");
 9         int num2=input.nextInt();
10         System.out.println("Before execution num1 is:"+num1+"   "+"num2:"+num2);
11         num1=num1^num2;
12         num2=num2^num1;
13         num1=num1^num2;
14         System.out.println("Later execution num1:"+num1+"   "+"num2:"+num2);
15     }
16 }

 

java-不用辅助变量,两变量直接交换