Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.mycompany.dz2;
- /**
- *
- * @author student
- */
- public class Main2 {
- /*
- Задача 7
- Создать метод, который находит
- среднее арифметическое элементов массива
- типа double[] - параметра данного метода
- Метод должен возвращать значение типа double
- В методе main проверить данный метод - вывести полученное значение на экран
- */
- //
- public static double avg(double[] arr){
- // 1)
- double sum = 0;
- for (int i = 0; i < arr.length; i++) {
- //sum+=arr[i];
- sum = sum + arr[i];
- }
- // 2)
- double result = sum / arr.length;
- return result;
- }
- //
- public static void main(String[] args) {
- //
- double[] arr = {1, 2.2, 5.5, 2.00};
- double result = avg(arr);
- System.out.println("result=" + result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement