public class main { //不像循环和选择,函数使用完,不对8种基本类型有效(值不变,因为只是值传递,本身地址没传过去) public static void main(String[] args) { main m=new main(); int a=10,b=20; m.exchange(10,20); System.out.println("使用完函数时,a的值为:"+a); } //函数不能写在main中,函数写在class{}中 public int exchange(int a,int b){ int temp; temp=a; a=b; b=temp; System.out.println("使用函数时,a的值为:"+a); return temp; } }
运行结果:
使用函数时:a的值为:20
使用完函数时,a的值为:10
Process finished with exit code 0