[프로그래머스/java]평균 구하기

리트리버J

·

2020. 12. 20. 14:48

728x90

정수를 담고 있는 배열 arr의 평균값을 return하는 함수, solution을 완성해보세요.

 

1
2
3
4
5
6
7
8
9
10
11
class Solution {
    public double solution(int[] arr) {
        double answer = 0;
        // for-each로 int배열 하나씩 더하기
        for(int temp : arr) {
            answer += temp;
        }
        // return 시 배열 길이만큼 나누기
        return answer / arr.length;
    }
}
cs

 

출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges

728x90