package cn.itcast.day05.demo04;
/*
一個方法可以有0、1、多個參數;但是只能有0或者1個返回值,不能有多個返回值。
如果希望一個方法當中產生了多個結果數據進行返回,怎么辦?
解決方案:使用一個數組作為返回值類型即可。
任何數據類型都能作為方法的參數類型,或者返回值類型。
數組作為方法的參數,傳遞進去的其實是數組的地址值。
數組作為方法的返回值,返回的其實也是數組的地址值。
*/
public class Demo02ArrayReturn {
public static void main(String[] args) {
int[] result = calculate(10, 20, 30);
System.out.println("main方法接收到的返回值數組是:");
System.out.println(result); // 地址值
System.out.println("總和:" + result[0]);
System.out.println("平均數:" + result[1]);
}
public static int[] calculate(int a, int b, int c) {
int sum = a + b + c; // 總和
int avg = sum / 3; // 平均數
// 兩個結果都希望進行返回
// 需要一個數組,也就是一個塑料兜,數組可以保存多個結果
/*
int[] array = new int[2];
array[0] = sum; // 總和
array[1] = avg; // 平均數
*/
int[] array = { sum, avg };
System.out.println("calculate方法內部數組是:");
System.out.println(array); // 地址值
return array;
}
}
