【JAVA】实现交换两个变量的值。要求:需要交换实参的值。

class Swap{
    private int num1;
    private int num2;

    public Swap(int num1,int num2){
        this.num1=num1;
        this.num2=num2;
    }

    public void func(){
        int temp;
        temp=this.num1;
        this. num1=this.num2;
        this.num2= temp;
        System.out.println("num1=" + num1);
        System.out.println("num2=" + num2);
    }
}


public class Test3 {
    public static void main(String[] args) {
        Swap a = new Swap(15,20);
        a.func();
    }
}

运行结果如图所示
在这里插入图片描述

发布了20 篇原创文章 · 获赞 23 · 访问量 568

猜你喜欢

转载自blog.csdn.net/m0_45097186/article/details/102727568