package com.Summer_0419.cn; /** * @author Summer * 获取数值型数组的平均值 */ public class Test_Method14 { public static void main(String[] args) { double[] a = { 100, 12, 102, 180, 13, 15.12 };// 静态方法声明一个变量 a.toString();//toString方法进行遍历 double b = avg(a); System.out.println("这是增强for循环返回的平均值"+b); double c = avg1(a); System.out.println("这是普通for循环返回的平均值"+c); } private static double avg1(double[] array) { double sum = 0; for (int i = 0; i < array.length; i++) { sum+=array[i]; } return sum/array.length; } private static double avg(double[] a) { double sum = 0; for (double d : a) { sum+=d; } return sum/a.length; } }
原文地址:https://www.cnblogs.com/summerdata/p/10739474.html
时间: 2024-11-10 08:15:31