Home > Java > javaTutorial > How to sum arrays in java?

How to sum arrays in java?

藏色散人
Release: 2020-09-17 15:36:12
Original
30019 people have browsed it

Arrays are one of the important data structures for every programming language. Of course, different languages ​​implement and process arrays differently. Arrays provided in the Java language are used to store fixed-size elements of the same type.

How to sum arrays in java?

How to sum a Java array?

Java array sum calculation:

Sum

Sum is to add on the basis of traversal A summation counter defines a sum variable outside the for. The initial value of this variable is 0.

In this way, the summation result is correct.

The purpose of writing the summation formula in the for loop is because the summation is also a process of adding up in the loop.

For example

double sum = 0;
int i;
for (i = 0; i < score.length; i++) {
sum = sum + score[i];
}
System.out.println(score.length + "位同学的成绩是" + sum);
Copy after login

Related learning recommendations: java basic tutorial

The above is the detailed content of How to sum arrays in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template