[Java] 학생별, 과목별 성적 평균 구하기

JTI·2022년 11월 20일
0

📌 Code list

목록 보기
26/55
import java.util.Arrays;

class ArrEx1 {
	// 평균 메서드
	public static void printArray(int[] arr) {
		int sum = 0;
		for(int n : arr) {
	 		sum += n;
		}
		System.out.println(sum / arr.length);
	}
	
	public static void main(String[] args) {
		int[][] scores = {{100, 80, 80, 100, 90}, 
						 {90, 70, 100, 90, 90}, 
						 {80, 80, 90, 70, 100}};
		
        //학생별 평균
		System.out.println("학생별 평균은: ");
		for(int[] score : scores) {
			printArray(score);
			System.out.println(Arrays.toString(score));
		}
        // 행 / 열 뒤집기
		System.out.println("과목별 평균은: ");
		int[][] subjects = new int[scores[0].length][scores.length];
		for(int i = 0; i < scores.length; i++) {
			for(int j = 0; j < scores[i].length; j++) {
				subjects[j][i] = scores[i][j];
			}
		}
        // 과목별 평균
		for(int[] subject : subjects) {
			printArray(subject);
			System.out.println(Arrays.toString(subject));
		}
	}
}


이렇게 행 / 열을 뒤집어서 과목별 평균을 구할 수 있다.

profile
Fill in my own colorful colors🎨

0개의 댓글