무룩 공부방

[Java] 44-2. 배열 (9) 성적표 조건 추가 (Eclipse) 본문

IT/Java01_Beginner

[Java] 44-2. 배열 (9) 성적표 조건 추가 (Eclipse)

moo_look 2023. 9. 5. 21:51

# 문제 보완

성적을 초기화된 2차원 배열이 아닌 과목별 점수를 직접 입력받는 방법으로 출력하라

과목별 평균, 최종 학점, 등수를  추가

 

 

<조건>

과목별 평균, 최종 학점을 추가하고

성적을 초기화된 2차원 배열이 아닌 직접 입력받아 출력하라

 

학점 :  90이상 A, 80이상 B, 70이상 C, 60이상 D, 60미만 F

 

<출력>

   번호   국어   영어   수학   총점    평균   학점   등수
     1       100    100    100     300    100        A        1
     2         50      50      50     150      50        F        2
     3         40      40      40     120      40        F        3
     4         10      10      10       30      10        F        4
---------------------------------------------------------------------
   총점    200     200     200

   평균      50      50       50


package java01_basic;

import java.util.Scanner;

public class Java44 {

	public static void main(String[] args) {
		// 2차원 배열 성적표를 만드시오
		// 학점 :  90이상 A, 80이상 B, 70이상 C, 60이상 D, 60미만 F
		// 번호	국어	영어	수학	총점	평균 학점 등수
		// 1	100	100	100	300	100  A	  1	
		// 2	50	50	50	150	50   F	  2
		// 3	40	40	40	120	40   F	  3
		// 4	10	10	10	30	10   F	  4
		// -------------------------------------
		// 총점	200	200	200
		// 평균	50	50	50
		
		Scanner sc = new Scanner(System.in);
		
		int[][] score = new int[4][3];
		
		int[] rank = {1,1,1,1};
		int[] rankSum = new int[4];
		int[] avg = new int[4];
		String[] hak = new String[4];
		
		int tkor = 0, teng = 0, tmat = 0, cnt = 0;
		
		for(int i = 0; i < score.length; i++) {
			
			cnt++;
			System.out.print(cnt+" 번 국어 영어 수학 점수 입력 : ");
			
			for(int j = 0; j < score[i].length; j++) {
				score[i][j] = sc.nextInt();
				rankSum[i] += score[i][j];
			}
			
			avg[i] = rankSum[i] / score[i].length;
			
			if(avg[i] >= 90) hak[i] = "A";
			else if(avg[i] >= 80) hak[i] = "B";
			else if(avg[i] >= 70) hak[i] = "C";
			else if(avg[i] >= 60) hak[i] = "D";
			else hak[i] = "F";
			
			tkor += score[i][0];
			teng += score[i][1];
			tmat += score[i][2];
			
		}
		for(int i = 0; i < rankSum.length; i++) {
			for(int j = 0; j < rankSum.length; j++) {
				if(rankSum[i] < rankSum[j]) rank[i]++;
			}
		}
		
		System.out.println("번호\t국어\t영어\t수학\t총점\t평균\t학점\t등수");
       		cnt = 0;
		
		for(int i = 0; i < score.length; i++) {
			
          		cnt++;
			System.out.print(cnt+"\t");
			
			for(int j = 0; j < score[i].length; j++) {
				System.out.print(score[i][j]+"\t");
			}
			System.out.println(rankSum[i]+"\t"+(int)avg[i]+"\t"+hak[i]+"\t"+rank[i]);
		}
		
		System.out.println("----------------------------------------------------");
		System.out.println("총점\t"+tkor+"\t"+teng+"\t"+tmat);
		System.out.println("평균\t"+tkor/cnt+"\t"+teng/cnt+"\t"+tmat/cnt);
		
		sc.close();
		
	}
}
이중 for문 - 데이터 입력 / 총점 / 평균 / 학점 
이중 for문 - 순위
이중 for문 - 출력

 

<출력>