- /**
- 功能:交换两个变量中的数值。
- */
- class TTT
- {
- public static void main(String[] args)
- {
- //方法一
- System.out.println("方法一:");
- int a=5,b=3;
- System.out.println("转换前a="+a+","+"b="+b);
- a=a^b;
- b=a^b;
- a=a^b;
- System.out.println("转换后a="+a+","+"b="+b);
- System.out.println();
- //方法二:
- System.out.println("方法二:");
- int c=7,d=9,temp=0;
- System.out.println("转换前c="+c+","+"d="+d);
- temp=c;
- c=d;
- d=temp;
- System.out.println("转换后c="+c+","+"d="+d);
- System.out.println();
- //方法三:
- System.out.println("方法三:");
- int i=3,j=7;
- System.out.println("转换前i="+i+","+"j="+j);
- i=i+j;
- j=i-j;
- i=i-j;
- System.out.println("转换后i="+i+","+"j="+j);
- System.out.println();
- //方法四:
- int o = 3,m = 5;
- System.out.println("转换前o="+o+","+"m="+m);
- o = m + (m = o) - m;
- System.out.println("转换后o="+o+","+"m="+m);
- }
- }
复制代码 |