java循环练习题-计算100以内偶数之和

1:for 循环:

package cn.work;

public class Work1 {
    
    
    public static void main(String[] args) {
    
    
        int n = 0;
        for (int i = 0; i <=100 ; i++) {
    
    
            if (i%2==0){
    
    
                n = n + i;
            }
        }
        System.out.println("100以内的偶数和为"+n);
    }
}

2:while 循环

package cn.work;

public class Work2 {
    
    
    public static void main(String[] args) {
    
    
        int i = 0;
        int n = 0;
        while(i<=100){
    
    
            i++;
            if(i%2==0){
    
    
                n = n+i;
            }
        }
        System.out.println("100以内的偶数和为"+n);
    }
}

3:do…while 循环

package cn.work;

public class Work2 {
    
    
    public static void main(String[] args) {
    
    
        int i = 0;
        int n = 0;
        while(i<=100){
    
    
            i++;
            if(i%2==0){
    
    
                n = n+i;
            }
        }
        System.out.println("100以内的偶数和为"+n);
    }
}

运算结果为
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/s001125/article/details/109785824