GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaArrays/AverageArray.md at main · AlexaJavaDev/JavaArrays

Expand file tree

/

AverageArray.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
25 lines (23 loc) · 828 Bytes
/

AverageArray.md

Copy path

File metadata and controls

25 lines (23 loc) · 828 Bytes
Raw
Copy raw file
Download raw file
Edit and raw actions

Что делает:
Считает среднее арифметическое элементов массива.

Код:

public class AverageArray {
    public static void main(String[] args) {
        int[] str = {1, 2, 3, 4, 5};
        System.out.println("Среднее: " + average(str));
    }

    public static double average(int[] str) {
        int sum = 0;  // начинаем с 0
        for (int i = 0; i < str.length; i++) {
            sum += str[i];  // добавляем каждый элемент к сумме
        }
        // (double) — превращаем сумму в дробное число, чтобы деление было точным
        return (double) sum / str.length;
    }
}

Пример вывода в консоли:

Среднее: 3.0
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.