프로그래밍 언어/JAVA

<JAVA>2차원 배열로 4학년 평점 구하기

창조적생각 2021. 6. 25. 17:00

 


public class example3_10 {

public static void main(String[] args) {
// TODO Auto-generated method stub
double score[][] = {{3.3, 3.4}, // 1학년 1, 2학기 평점
{3.5, 3.6}, // 2학년 1, 2학기 평점
{3.7, 4.0}, // 3학년 1, 2학기 평점
{4.1, 4.2}  // 4학년 1, 2학기 평점
};
double sum = 0;
for(int year = 0; year<score.length; year++) //각 학년별로 반복
for(int term = 0; term<score[year].length;term++) // 학기별로 반복
sum += score[year][term];

int n = score.length; // 배열의 행 개수, 4 (학년 수)
int m = score[0].length; // 배열의 열 개수, 2(학기 수) 
System.out.println("4학년 전체 평점 평균은" + sum/(n*m));
}

}

실행 결과
4학년 전체 평점 평균은3.725

728x90