【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();
    }
}

運行結果如圖所示
在這裏插入圖片描述

發佈了34 篇原創文章 · 獲贊 29 · 訪問量 3690
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章