Java/배열

배열(11)

은찡안찡 2023. 4. 3. 16:11
package com.dream.array;
//학생별(6명), 과목별(국어, 영어, 수학, 과학) 총점과 평균을 구하는 프로그램
public class ArrayEx11 {
	public static void main(String[] args) {
		//2차원 배열(6행 4열)
		int[][] score = {
			{76,80,92,100},
			{92,80,76,72},
			{75,72,84,95},
			{56,55,65,68},
			{88,80,92,95},
			{80,95,88,85}
		};
		
		int[] studentTotal = new int[6]; //학생별 총점을 담을 배열을 선언한다.
		int[] studentAvg = new int[6]; //학생별 평균을 담을 배열을 선언한다.
		int[] subjectTotal = new int[4]; //과목별 총점을 담을 배열을 선언한다.
		int[] subjectAvg = new int[4]; //과목별 평균을 담을 배열을 선언한다.
		
		System.out.println("\n===== 과목별 총점과 평균 =====");
		for(int i=0; i<4; i++) {
			for(int j=0; j<6; j++) {
				subjectTotal[i] += score[j][i]; //과목별 학생점수를 누적시킨다.
				
				if(j == 5) {
					subjectAvg[i] = subjectTotal[i] / 6;
				}
			}
		}
		
		String[] subject = {"국어", "영어", "수학", "과학"};
		for(int i=0; i<subject.length; i++) {
			System.out.println(subject[i]+"의 총점: "+subjectTotal[i]);
			System.out.println("평균: "+subjectAvg[i]);
		}
		
	}
}

'Java > 배열' 카테고리의 다른 글

배열(9)  (0) 2023.04.03
배열(8)  (0) 2023.04.03
배열(7)  (0) 2023.04.03
배열(6)  (0) 2023.04.03
배열(5)  (0) 2023.04.03