Java/Array(배열)

2차원 배열로 성적출력

Bohemian life 2012. 4. 9. 13:56
public class Score {//2차원 배열로 성적 출력
	public static void main(String[] args){
		int[][] score = {
				{100,100,100},
				{20,20,20},
				{30,30,30},
				{40,40,40},
				{50,50,50}
		};

		System.out.println("번호 국어 영어 수학 총점 평균");
		System.out.println("==================");

		for(int i=0; i <score.length; i++){
			int sum=0;
			System.out.print(" " + (i + 1) + " ");
			//i + 1은 번호를 출력하기 위함 +1안하면 0 으로됨
			for(int j=0;j < score[i].length;j++){
				sum+=score[i][j]; //총점 얻기위한 누적
				System.out.print(score[i][j]+" ");//과목 점수 출력
			}
			System.out.println(sum + " " + sum/score[i].length);
			//총점 나누기 과목갯수로 평균값 구하기
		}
	}
}