【程式語言】JAVA的第六堂課 |在方法中呼叫方法 印出陣列並算出平均
Published:
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;
}
}