【发布时间】:2017-03-27 10:19:03
【问题描述】:
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
【问题讨论】:
标签: java class methods constructor scope