본문 바로가기
IT/Java

Java (2차원 배열_성적처리 프로그램)

by hjshims 2021. 5. 14.

 

 

package p0514;

import java.util.Scanner;

public class Array2_Score {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		int i=0, j=0;
		
		System.out.print("인원수를 입력하세요: ");
		int inwon = scan.nextInt();
		
		String[] subjectname = {"국어", "영어", "수학"};
		String[] name = new String[inwon];
		
		int[][] subject = new int[inwon][subjectname.length+2];	//[입력한인원수][5](국,영,수,석차,합계)
		double[] avg = new double[inwon];	//평균
		
		for(i=0; i<inwon; i++) {
			System.out.printf("%d번째 사람이름을 입력하세요: ", i+1);
			name[i]=scan.next();	//이름 입력하여 배열에 저장
			for(j=0; j<subjectname.length; j++) {
				System.out.printf("%s점수 = ",subjectname[j]);
				subject[i][j] = scan.nextInt();	//점수입력 (국,영,수)
				
				subject[i][subjectname.length] += subject[i][j];	//누적 합계
			}
			avg[i] = subject[i][subjectname.length]/3;	//평균
			subject[i][subjectname.length+1]=1;	//석차 (총합을 다 1로 만듦)
		}
		
			for(i=0; i<inwon; i++) {
				for(j=0; j<inwon; j++) {
					if(subject[i][subjectname.length] < subject[j][subjectname.length]) {		
						//누적 합계 배열 안에 있는 점수를 계산해서 나보다 점수가 크면 내 석차에 1증가(등수가 밀려나는거)
						subject[i][subjectname.length+1]++;
					}
				}
			}

		System.out.println("====================성적처리 결과======================");
		System.out.println("이름\t국어\t영어\t수학\t석차\t합계\t평균");
		System.out.println("====================================================");
		for(i=0; i<name.length; i++) {
			System.out.printf("%s\t%3d\t%3d\t%3d\t%3d\t%3d\t%.1f\n",name[i],
					subject[i][j],subject[i][j],subject[i][j],
					subject[i][subjectname.length+1],
					subject[i][subjectname.length],avg[i]);
		}
		System.out.println("===================성적처리 결과 끝=====================");
	}

}

 

<출력결과>