yueyue~

导航

for循环计算0-100之间奇数和偶数的和—Java

package Struct;

public class ForDemo02 {
    public static void main(String[] args) {
        int oddSum = 0;
        int evenSum = 0;

        for (int i = 0; i <= 100; i++) {
            if(i%2 != 0){ //奇数
                oddSum += i; //oddSum =oddSum + i
            }else { //偶数
                evenSum += i; //evenSum = evenSum + i
            }
        }

        System.out.println("奇数的和:" +oddSum);
        System.out.println("偶数的和:" +evenSum);
    }
}

运行结果

posted on 2021-08-12 17:07  yueyue~  阅读(728)  评论(0编辑  收藏  举报