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/ProductOfArray.md at main · AlexaJavaDev/JavaArrays

Expand file tree

/

ProductOfArray.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
32 lines (27 loc) · 842 Bytes
/

ProductOfArray.md

Copy path

File metadata and controls

32 lines (27 loc) · 842 Bytes
Raw
Copy raw file
Download raw file
Outline
Edit and raw actions

Что делает:
Перемножает все элементы массива (вычисляет произведение).

Код:

public class ProductOfArray {
    public static void main(String[] args) {
        int[] str = {1, 2, 3, 4, 5};
        System.out.println(massiv(str));
    }

    public static int massiv(int[] str) {
        int res = 1;

        for (int i = 0; i < str.length; i++) {
            res *= str[i];
        }
        return res;
    }
}

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

120

Мои заметки:

  • Для суммы мы начинали с 0, а для произведения — с 1 (умножение на 0 обнулит всё!)
  • res *= str[i] — то же самое, что res = res * str[i]
  • 1 × 2 × 3 × 4 × 5 = 120
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.