tzkr.net
当前位置:首页 >> jAvA计算平均值 >>

jAvA计算平均值

public class Average{ public static void main(String[] args){//自己定义要求的数 double[] nums = {1, 2.5, 5}; System.out.println("平均数为:" + average(nums)); } public static double average(double[] nums){ double num=0.0; for(int i=0; i<nums.length; i++){ num+=nums[i]; } return num/nums.length; } }

int[] array = new int[]{2,8,4,9,5,2,4,7,8,3,4,2,2,1,3,45,5};//数组 double sum = 0;//用来保存数组内所有数值的和 for(int i=0;i<array.length;i++){ sum+=array[i];//遍历每一个数组相,将每一个数组相的值加到sum上 } double avg = sum/array.length;//计算平均值:总和除以总个数 System.out.println(avg);//打印计算结果

import java.util.*; public class NumOption { public static void main(String[] args) { int sum = 0; int i = 0; int count= 0; Scanner sc = new Scanner(System.in); System.out.println("请输入整数,以0结尾"); do { System.out.print("请输入数字:"

------------------接口----------------------------- public interface Average { public double avg(double[] params); }--------------------first1-------------------------- public class First implements Average { /** * 求传入数组的平均值 * */ public double avg(double[] params)

package test;import java.util.List;public interface Average{ public double getAverage(List list);}package test;import java.util.List;public class First implements Average { public double getAverage(List list) { double sum = 0; for(int i=0;i list) { Collections.

你想求什么的平均值?abc的平均值 sum?sum=(a+b+c)/3; system.out.println("a+b+c的平均值是:"+sum);

//test public static void main(String[] args) { int[] ti={12,51,23,4,72,34,7,25,78,61}; cxsz.testNumber(ti); } public class cxsz {//求最小、最大、平均、和 public static void testNumber(int[] par) { int min = par[0], max = par[0], sum = 0, ave = 0; for (int i = 0;

import java.util.scanner;public class mydao { public static void main(string args[]){ scanner sc=new scanner(system.in); system.out.println("请输入第1个数:"); int a=sc.nextint(); system.out.println("请输入第2个数:"); int b=sc.nextint(

用个循环就是了

package test;import java.util.ArrayList;import java.util.List;public class test { public static void main(String[] args) { getAverage(-1,2,3,4,101); } public static void getAverage(int num){ int count = 0; List list = new ArrayList(); for(int i = 0; i < num.length;

网站首页 | 网站地图
All rights reserved Powered by www.tzkr.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com