集册 Java实例教程 计算数组元素的平均值

计算数组元素的平均值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

482
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
计算数组元素的平均值

public class Main {

 

        public static void main(String[] args) {/*时代Java - N o w  J a v a . c o m 提供*/

               

                int[] numbers = new int[]{110,120,115,25,116,60,110};

               

                int sum = 0;

               

                for(int i=0; i < numbers.length ; i++)

                        sum = sum + numbers[i];

               

                //calculate average value

                double average = sum / numbers.length;

               

                System.out.println("Average value of array elements is : " + average);

        }

}