java中如何实现具有交换两个整数值功能的函数
下面代码无法实现交换public class SwapFuncation {
/**
* 使用int做型参
*/
public void swapInt(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
}
public static void main(String[] args) {
int x = 1, y = 5;
SwapFuncation s = new SwapFuncation();
System.out.println ("交换前");
System.out.println (x);
System.out.println (y);
s.swapInt(x, y);
System.out.println ("交换后");
System.out.println (x);
System.out.println (y);
}
}
/**
* 使用int做型参
*/
public void swapInt(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
}
public static void main(String[] args) {
int x = 1, y = 5;
SwapFuncation s = new SwapFuncation();
System.out.println ("交换前");
System.out.println (x);
System.out.println (y);
s.swapInt(x, y);
System.out.println ("交换后");
System.out.println (x);
System.out.println (y);
}
}