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

运算结果为
java循环练习题-计算100以内偶数之和_第1张图片

你可能感兴趣的:(循环,java)