java中实现两个数字的交换

发布时间:2024年01月19日

java中实现两个数字的交换

package pro1;

class Num{
    private int num;

    public Num(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

public class M1 {



    static void switch_(Num n1,Num n2){
        int temp = n1.getNum();
        n1.setNum(n2.getNum());
        n2.setNum(temp);
    }

    public static void main(String[] args) {
        Num n1 = new Num(10);
        Num n2 = new Num(20);

        System.out.println("Before swapping, n1 = " + n1.getNum() + " and n2 = " + n2.getNum());
        switch_(n1, n2);
        System.out.println("After swapping, n1 = " + n1.getNum() + " and n2 = " + n2.getNum());
    }

}

文章来源:https://blog.csdn.net/laocooon/article/details/135691937
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。