|NO.Z.00064|——————————|BigDataEnd|——|Java&循环结构.V03|——|Java.v03|for循环.v03|实现累加和|

一、for循环实现累加和
### --- 案例题目

~~~     ——>    使用for循环实现累加:1+2+...+10000=?最后打印出来。
二、编程代码
### --- 编程代码

/*
    编程使用for循环实现1 ~ 10000之间所有整数的累加和
 */
public class ForSumTest {
    
    public static void main(String[] args) {
        
        // 2.声明一个变量负责记录累加的结果
        int sum = 0;
        
        // 1.使用for循环打印1 ~ 10000之间的所有整数
        for(int i = 1; i <= 10000; i++) {
            // 打印后不换行
            //System.out.print(i + " ");
            // 将所有i的取值都累加到变量sum中
            sum += i; // sum = sum + i;
        }
        // 专门用于换行
        //System.out.println();
        
        // 3.打印最终的累加结果
        System.out.println("sum = " + sum);
    }
}
三、编译打印
### --- 编译

C:\Users\Administrator\Desktop\project>javac ForSumTest.java
### --- 打印输出

C:\Users\Administrator\Desktop\project>java ForSumTest
sum = 50005000

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on 2022-04-02 23:12  yanqi_vip  阅读(19)  评论(0)    收藏  举报

导航