zoukankan      html  css  js  c++  java
  • 求数组的平均值 Exercise07_08

     1 import java.util.Scanner;
     2 /**
     3  * @author 冰樱梦
     4  * 时间:2018年下半年
     5  * 题目:求数组的平均值
     6  *
     7  */
     8 public class Exercise07_08 {
     9     public static void main(String[] args){
    10         Scanner input=new Scanner(System.in);
    11         double[] array=new double[10];
    12         System.out.println("输入10个double型的数");
    13         for(int i=0;i<array.length;i++){
    14             array[i]=input.nextDouble();
    15         }
    16         System.out.print(average(array));
    17     }
    18     
    19     //求int类型的数组的平均值
    20     public static int average(int[] array){
    21         int total=0;
    22         for(int i=0;i<array.length;i++){
    23             total+=array[i];
    24         }
    25         return total/array.length;
    26     }
    27     
    28     
    29     //求double类型的数组的平均值
    30     public static double average(double[] array){
    31         double total=0;
    32         for(int i=0;i<array.length;i++){
    33             total+=array[i];
    34         }
    35         return total/array.length;
    36     }
    37 }
  • 相关阅读:
    排名第一、第二的OCR软件
    补码输出
    枚举 与 枚举的应用
    动态构造结构体数组
    c 冒泡排序
    strcpy
    typedef用法
    C 结构体小结
    int 占一个机器字长
    SQL Server创建视图——视图的作用
  • 原文地址:https://www.cnblogs.com/cherrydream/p/10174066.html
Copyright © 2011-2022 走看看