(국비)2차원 배열을 사용한 성적 처리

DeokHun KIM·2022년 7월 13일
0

java

목록 보기
16/30

// 국어, 영어, 수학 점수 3개를 저장한 2차원 배열(sungjuk) 만들고
// 3명 성적을 입력하고 개인별 총점과 평균을 계산하여 화면에 데이터 출력

/*
국어\t영어 수학 총점 평균
--------------------
100 90 80 270 90.0
100 90 81 271 90.33
100 90 80 270 90.0
====================*/
        
int[] sungjuk = {100, 90, 80}; //한 사람
int[][] sungjuk = {{100, 90, 80},    
				   {100, 90, 81}, 
        	       {100, 90, 80} }; //세 사람
 //100: score[0][0][0], 90:[0][1][0], 80:[0][0][2]
//100: score[1][0][0], 90:[1][1][0], 81:[1][0][2]
//100: score[2][0][0], 90:[2][1][0], 80:[2][0][2]

int kor = 0;
int eng = 0;
int math = 0;
int tot = 0;
double avg = 0;                 
// 첫번째 데이터 처리 -----------------------------------
System.out.println(sungjuk[0][0]);  //100
kor = sungjuk[0][0];
eng = sungjuk[0][1];
math = sungjuk[0][2];
tot = kor + eng + math;
avg = tot * 100 / 3 / 100.0;

System.out.println("국어\t영어\t수학\t총점\t평균");  // \t tap만큼 떨어져라
System.out.println("-----------------------------------");
System.out.println(kor + "\t" + eng + "\t" + math + "\t" + tot + "\t" + avg);

국어 영어 수학 총점 평균
100 90 80 270 90.0

// 2번째 데이터 처리
kor = sungjuk[1][0];
eng = sungjuk[1][1];
math = sungjuk[1][2];
tot = kor + eng + math;
avg = tot * 100 / 3 / 100.0; 
System.out.println(kor + "\t" + eng + "\t" + math + "\t" + tot + "\t" + avg);
        
// 3번째 데이터 처리
kor = sungjuk[2][0];
eng = sungjuk[2][1];
math = sungjuk[2][2];
tot = kor + eng + math;
avg = tot * 100 / 3 / 100.0;
System.out.println(kor + "\t" + eng + "\t" + math + "\t" + tot + "\t" + avg);

100 90 81 271 90.33
100 90 80 270 90.0

  • 반복문으로 만들기
for (int i = 0; i <sungjuk.length; i++) {
    kor = sungjuk[i][0];
    eng = sungjuk[i][1];
    math = sungjuk[i][2];
    tot = kor + eng + math;
    avg = tot * 100 / 3 / 100.0;
    System.out.println(kor + "\t" + eng + "\t" + math + "\t" + tot + "\t" + avg);
}
  • 줄 긋기 메소드 활용
static final String TITLE = "국어\t영어\t수학\t총점\t평균";
static final String LINE = "-----------------------------------";

static void dispTitle() {
	System.out.println(LINE);
	System.out.println(TITLE);
	System.out.println(LINE);
}

0개의 댓글