ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 5. array - (6) ArrayDemo6 (2차원 배열-점수 출력)
    java/코드 리뷰 2020. 3. 23. 17:58
    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
    package array;
     
    public class ArrayDemo6 {
        public static void main(String[] args) {
            
            int[][] scores = {
                    {806070}, // 김유신의 국어, 영어, 수학
                    {304050}, // 강감찬의 국어, 영어, 수학
                    {10086100// 이순신의 국어, 영어, 수학
            };
            
            int korTotal = 0;
            int engTotal = 0;
            int mathTotal = 0;
            
            System.out.println("번호    국어    영어    수학    총점    평균");
            System.out.println("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ");
            
            for(int i=0; i<scores.length; i++) {
                System.out.print(i+1+"\t"); // 번호 출력
                korTotal += scores[i][0]; // 0번행, 1번행, 2번행의 국어점수 누적
                engTotal += scores[i][1]; // 0번행, 1번행, 2번행의 영어점수 누적
                mathTotal += scores[i][2]; // 0번행, 1번행, 2번행의 수학점수 누적
                
                int total = 0// 0번행, 1번행, 2번행의 총점을 저장하는 변수(한 회전이 끝날때마다 total 값을 초기화)
                for(int j=0; j<scores[i].length; j++) {
                    total += scores[i][j]; // 각 행별 과목점수를 total에 누적
                    System.out.print(scores[i][j]+"\t"); // 각 행별 과목점수를 출력
                }
                System.out.println(total + "\t" + total/3 + "\t"); // 각 행별 총점과 평균을 출력
                
                System.out.println();
            }
            System.out.println("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ");
            System.out.println("총점    " + korTotal + "\t" + engTotal + "\t" + mathTotal);
        }
    }
     
Designed by Tistory.