package interview_programs;
public class Swapping {
int x,y ;
Swapping(int a, int b){
int x = a;
int y = b;
System.out.println("the valueof x is" + x);
System.out.println("the valueof y is" + y);
}
public void replace(){
x = x + y;
y = x - y;
x = x - y;
System.out.println("the value after swap");
System.out.println("the valueof x is" + x);
System.out.println("the valueof y is" + y);
}
public static void main (String args[]) {
Swapping Swap = new Swapping(10,5);
Swap.replace();
}
}
这是控制台上的输出。
输出:
x 的值为 10
y 的值为 5
交换后的值
x的值为0
y的值为0
最佳答案
您不会初始化类级别的 x 和 y,而是初始化构造函数中的局部 x 和 y。
将“int x = a”替换为“x = a”,将“int y = b”替换为“y = b”。
关于java - 在替换方法中没有获取值 x 和 y,即(10 和 5)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40577556/