Home > Java > javaTutorial > How to use the for loop statement in java to calculate the cumulative sum of 1~100

How to use the for loop statement in java to calculate the cumulative sum of 1~100

王林
Release: 2020-04-28 15:24:50
Original
26325 people have browsed it

How to use the for loop statement in java to calculate the cumulative sum of 1~100

Ideas:

1. First declare a variable to save the accumulated sum, such as [int sum=0];

2. Then Just use a for loop statement to perform cumulative calculations, such as [for(int x=0;x<=10;x){sum =x}].

(Recommended video tutorial: java video)

Code implementation:

public class for实现 {
    public static void main(String[] args)
    {
        //用for循环实现1-10的和;
        int sum=0;
        for(int x=0;x<=10;x++)
        {
            sum+=x;
        }
        System.out.println("使用for循环得到的sum是:"+sum);
    }
}
Copy after login

Recommended tutorial: java entry program

The above is the detailed content of How to use the for loop statement in java to calculate the cumulative sum of 1~100. 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