본문 바로가기
프로그래밍 언어/자바의 정석 기초편

자바의 정석 기초편 ch5-21~23 2차원배열 예제

by life grow 2022. 6. 15.

5-21 2차원 배열 예제

 

score.length = 4

score[0], score[1], score[2], score[3]

score[i].length = 3

score[0][0], score[0][1], score[0][2]

 

score[i]에 score[0]이라고 놔도 됨

왜냐하면 지금은 다 길이가 같으니까

근데 배열의 길이가 다를 수 있기 때문에 i로 둔 것이다.

 

예 5-8

public class lnterfaceEx {
	public static void main(String[] args) {		
		
		int[][] score = {
						    {100, 100, 100}
						  , {20, 20, 20,5}
						  , {30, 30, 30}
						  , {40, 40, 40}
						};
		int sum = 0;
		
		for(int i=0;i<score.length;i++) {
			for(int j=0;j<score[i].length;j++) {
				sum += score[i][j];
				System.out.println("score["+i+"]["+j+"]="+score[i][j]);
			}
		}
		
		System.out.println(sum);
	}
}
결과
score[0][0]=100
score[0][1]=100
score[0][2]=100
score[1][0]=20
score[1][1]=20
score[1][2]=20
score[1][3]=5
score[2][0]=30
score[2][1]=30
score[2][2]=30
score[3][0]=40
score[3][1]=40
score[3][2]=40
575

{100, 100,}
  , {20, 20, 20,5}
  , {30, 30}
  , {40}

2차원 배열을 위와 같이 만들수도 있다.

 

 

예제 5-9

평균 낼 때는 float로 형변환 하는 거 잊지말자

public class lnterfaceEx {
	public static void main(String[] args) {		
		
		int[][] score = {
						    {100, 100, 100}
						  , {20, 20, 20}
						  , {30, 30, 30}
						  , {40, 40, 40}
						  , {50, 50, 50}
						};
		//과목별 총점
		int korTotal = 0, engTotal = 0, mathTotal = 0;
		
		System.out.println("번호 국어 영어 수학 총점 평균");
		System.out.println("================================");
		
		for(int i=0;i<score.length;i++) {
			int sum = 0;       //개인별 총점
			float avg = 0.0f;  //개인별 평균
			
			korTotal += score[i][0];
			engTotal += score[i][1];
			mathTotal += score[i][2];
			System.out.printf("%3d", i+1);
			
			for(int j=0;j<score[i].length;j++) {
				sum += score[i][j];
				System.out.printf("%5d", score[i][j]);
			}
			
			avg = sum/(float)score[i].length;  //평균계산
			System.out.printf("%5d %5.1f%n", sum, avg);
		}
		
		System.out.println("================================");
		System.out.printf("총점:%3d %4d %4d%n ", korTotal, engTotal, mathTotal);
	}
}
 결과
 번호 국어 영어 수학 총점 평균
================================
  1  100  100  100  300 100.0
  2   20   20   20   60  20.0
  3   30   30   30   90  30.0
  4   40   40   40  120  40.0
  5   50   50   50  150  50.0
================================
총점:240  240  240

new int[5][3];

[5] == score.length

[3] == score[i].length

(예제가 이해 안가면 코드를 조금씩 수정해가면서 이해해보자)

 

예제 5-10

import java.util.Scanner;

public class lnterfaceEx {
	public static void main(String[] args) {		
		
		String[][] score = {
						    {"chair", "의자"}
						  , {"computer", "컴퓨터"}
						  , {"integer", "정수"}
						};
			
		Scanner scanner = new Scanner(System.in);
		for(int i=0;i<score.length;i++) {
			System.out.printf("Q%d. %s의 뜻은?", i+1, score[i][0]);
			
			String tmp = scanner.nextLine();
			
			if(tmp.equals(score[i][1])) {
				System.out.printf("정답입니다.%n%n");
			}else {
				System.out.printf("틀렸습니다. 정답은 %s입니다.%n%n", score[i][1]);
			}
		}	
	}
}
결과
Q1. chair의 뜻은?ㄱ
틀렸습니다. 정답은 의자입니다.

Q2. computer의 뜻은?컴퓨터
정답입니다.

Q3. integer의 뜻은?정수
정답입니다.

 

\n 줄바꿈 문자가 os마다 다르기 때문에

%n을 쓴다.