一行代码是不行的 除非整个方法 下面是有三种交换变量的值的方法
- public static void exchange1(int a, int b) {
- int temp;
- temp = a;
- a = b;
- b = temp;
- System.out.println("a="+ a + "b = "+b);
-
- }
- public static void exchange2(int a, int b) {
- a = a + b;
- b = a - b;
- a = a - b;
- System.out.println("a="+ a + "b = "+b);
- }
- public static void exchange3(int a, int b) {
- a = a ^ b;
- b = a ^ b;
- a = a ^ b;
- System.out.println("a="+ a + "b = "+b);
- }
复制代码 |