※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

63 瀏覽人次

【程式語言】JAVA的第六堂課 |在方法中呼叫方法 印出陣列並算出平均

Published: in JAVA by .

public class hw7_22 {
    public static void main(String[] args) {
        int[] arr = {75,29,10,38,45,17};
        System.out.print("AVERAGE:" + avg(arr));       
    }

    public static void show(int arr[]) {
        System.out.print("arr[] = { ");
        for (int i = 0 ; i< arr.length ; i++) {
            System.out.print(arr[i] + " ");
    }
        System.out.print("}");
}
    public static double avg(int arr[]) {
        int sum = 0;
        show(arr);
        for (int i = 0 ; i < arr.length ; i++) {
            sum += arr[i];
        }
        System.out.println();
        return sum/arr.length;
    }
}

Result:

©2019 - 2024 Henry Chen