본문 바로가기
Dev/Java

1. 합계 알고리즘 (Sum Algorithm)

by vellahw 2022. 8. 8.

 

//[?] n명의 국어 점수 중에서 80점 이상인 점수의 합계
/**
 * 합계 알고리즘: 주어진 범위에 주어진 조건에 해당하는 자료들의 합계
 */

public class SumAlgorithm2 {
    public static void main(String[] args) {
        int[] scores = { 100, 75, 50, 37, 90, 95 };
        int sum = 0; // 결과값 담김
        
        for (int i = 0; i < scores.length; i++) {  // 범위 지정(=> 0에서 scores.length까지)
            if(scores[i] >= 80) {  // 만약에 scores배열의 i 번째 데이터가 80보다 크거나 같다면
                sum += scores[i];  // SUM 영역 (=> sum에 누적을 시켜라)
            }
        }

        System.out.println(scores.length + "명의 점수 중 80점 이상의 총점: " + sum);
    }
}

 

출력값: 6명의 점수 중 80점 이상의 총점: 285

댓글