1. 程式人生 > >for迴圈實現1-100之間偶數和

for迴圈實現1-100之間偶數和

package com.itheima_04;
/*
 * 需求:求出1-100之間偶數和
 * 
 * 分析:
 *      A:定義求和變數,初始化值是0
 *      B:獲取1-100之間的資料,用for迴圈實現
 *      C:把獲取到的資料進行判斷,看是否是偶數
 *          如果是,就累加
 *      D:輸出求和結果
 */
public class ForTest3 {
    public static void main(String[] args) {
        //定義求和變數,初始化值是0
        int sum =0;
      //獲取1-100之間的資料,用for迴圈實現
        for(intx=1; x<=100;x++) {
            //把獲取到的資料進行判斷,看是否是偶數
            if(x%2 ==0){
                sum += x;
            }
        }        
        //輸出求和結果
        System.out.println("sum:"+sum);
    }
}