3초기억력

배열 : 2차원배열 본문

자바_기초

배열 : 2차원배열

잠수콩 2019. 5. 21. 15:24
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
// 2차원배열
// 제기차기. 1,2,3학년 각 3명씩, 총합갯수, 평균구하기

public class Array2_3 {

	public static void main(String[] args) {


		double[][] score = new double[3][3];
		double total = 0;
		double avg = 0.0;
		double cnt = 0;

		score[0][0] = 11;
		score[0][1] = 20;
		score[0][2] = 30;

		score[1][0] = 100;
		score[1][1] = 200;
		score[1][2] = 300;

		score[2][0] = 12;
		score[2][1] = 21;
		score[2][2] = 3;
		
		System.out.println(score.length);			//길이 3 나옴.
		
		//총합 구하기
		for (int i=0; i<3;i++) {
			
			for(int j=0; j<3; j++) {
				System.out.println((i+1) + "학년의 " + (j+1) + "번째 학생은 " + score[i][j] + "개");
				total = total + score[i][j];
				cnt++;
			}
			
		}
		
		System.out.println("총합은 "+ total +"입니다.");

		//avg = (double)total / (double)cnt;
		avg = total / cnt;
		
		System.out.println("평균은 "+ avg +"입니다.");
		System.out.println("평균 : " + String.format("%.2f", avg));
		

	}

}
Comments