java achieve a method avg, an array of parameters, find the average of all elements in the array (note that the return value type).

public class Test41{
    public static void main(String[] args){
        int[] arr={1,2,3,4,5,6,7};
        System.out.println(avg(arr));
    }
    public static double avg(int[] a){
        int ret=0;
        double sum;
        for(int x:a){
            ret+=x;
        }
        sum=ret/a.length;
        return sum;
    }
}

Published 87 original articles · won praise 2 · Views 713

Guess you like

Origin blog.csdn.net/Nabandon/article/details/103756708