|
package xiangmu;
|
|
|
|
public class Ceshi {
|
|
public static void main(String args[]) {
|
|
int a = 3;
|
|
int b = 5;
|
|
int c = 0;
|
|
dayin("交换前:");
|
|
dayin("a: " + a);
|
|
dayin("b: " + b);
|
|
c = a; // 通过临时变量c存储变量a中的值,避免被直接覆盖
|
|
a = b;
|
|
b = c;
|
|
dayin("交换后:");
|
|
dayin("a: " + a);
|
|
dayin("b: " + b);
|
|
}
|
|
public static void dayin(Object obj) {
|
|
System.out.println(obj);
|
|
}
|
|
}
|
交换前:
a: 3
b: 5
交换后:
a: 5
b: 3