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

How to sum arrays in java?

藏色散人
藏色散人Original
2019-05-22 11:54:0829999browse

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);

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!

Statement:
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