Home >Java >javaTutorial >How to sum arrays in java?
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 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!