在这个java程序中,我想实现两个数字的交换,但是无法实现,应该怎么改正那个change函数呢

来源:百度知道 编辑:UC知道 时间:2024/05/18 02:39:47
public class TestChange {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=9;
int b=1;
change(a,b);
System.out.println(a);
System.out.println(b);

}
public static void change(int a,int b){
int c=0;
c=a;
a=b;
b=c;
}

}
请给出正确代码,不要在函数前定义变量a,b

package testchange;

public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=9;
int b=1;
change(a,b);
}
public static void change(int a,int b){
int c=0;
c=a;
a=b;
b=c;
System.out.println("a="+a);
System.out.println("b="+b);
}

}

public class TestChange {

/**
* @param args
*/
int a,b;
public static void main(String[] args) {
// TODO Auto-generated method stub
a=9;
b=1;
change();
System.out.println(a);
System.out.println(b);

}
public static void change(){
int c=0;
c=a;
a=b;
b=c;
}

}

函数传递有2种 一种是 数值 一种是 引用

你所写的函数用的是第一种,So..

你change方法中改变只是该方法中的 a、b
public class TestChange {
private static int a=0;
pr