본문 바로가기

JAVA를 잡아라!/Java

성적관리1 - 배열을 이용한 총점, 평균 계산

class SungJuk1
{
      public static void main(String[] args)
      {
            int[][] score = {{ 100, 100, 100}
                                    , { 20, 20, 20}
                                    , { 30, 30, 30}
                                    , { 40, 40, 40}
                                    , { 50, 50, 50}};
            int koreanTotal = 0;       // 국어총점
            int englishTotal = 0;       // 영어총점
            int mathTotal = 0;             // 수학총점

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

            for(int i=0;i < score.length;i++) {
                  int sum=0;
                  koreanTotal += score[i][0];
                  englishTotal += score[i][1];
                  mathTotal += score[i][2];
                  System.out.print(" " + (i + 1) + " ");
                  for(int j=0;j < score[i].length;j++) {
                        sum+=score[i][j];
                        System.out.print(score[i][j]+" ");
                  }

                  // 소숫점아래의 평균을 얻기위해 float로 변환해서 나눈다.
                  System.out.println(sum + " " + sum/(float)score[i].length);
            }
      System.out.println("=============================");
      System.out.println("총점:" + koreanTotal + " " +englishTotal +" " +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

*/