Home  >  Article  >  Java  >  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

王林
王林Original
2020-04-28 15:24:5026233browse

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

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!

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