자바 루프 연습-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