ABOUT ME

95.12.1 / 취준생 / Java

Today
Yesterday
Total
  • 5. array - (5) ArrayDemo5 (2차원 배열)
    java/코드 리뷰 2020. 3. 23. 17:56
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    package array;
     
    public class ArrayDemo5 {
        public static void main(String[] args) {
            
            // 2차원 배열
            int[][] scores = new int[2][3]; // 2행 3열
            // 0번행은 이순신의 국영수 점수
            scores[0][0= 60;
            scores[0][1= 80;
            scores[0][2= 90;
            // 1번행은 김유신의 국영수 점수
            scores[1][0= 50;
            scores[1][1= 40;
            scores[1][2= 40;
            
            System.out.println("scores.length --> " + scores.length);
            System.out.println("scores[0].length --> " + scores[0].length);
            System.out.println("scores[1].length --> " + scores[1].length);
            
            for(int i=0; i<scores.length; i++) {
                for(int j=0; j<scores[i].length; j++) {
                    System.out.println(i + "," + j + " -----> " + scores[i][j]);
                }
            }
            
            // 이순신의 총점
            int total1 = 0;
            for (int i=0; i<3; i++) {
                System.out.println(scores[0][i]);
                total1 += scores[0][i];
            }
            System.out.println("이순신의 총점 : " + total1);
            
            // 김유신의 총점
            int total2 = 0;
            for (int i=0; i<3; i++) {
                System.out.println(scores[1][i]);
                total2 += scores[1][i];
            }
            System.out.println("김유신의 총점 : " + total2);
            
            
        }
    }
     
Designed by Tistory.