본문 바로가기
유수봉 교수님 자바

[Java] 점수 3개에 대한 합계, 평균, 등급, 석차 처리후 정렬

by seung_nari 2023. 10. 28.
import java.util.Scanner;

public class Main
{
    public static void main(String[] args){
        String Name[] = {"김승종", "박상근", "신동인", "권승혁", "허윤서", "정류", "홍기태", "박현진", "김재호", "박세훈"};
        String SID[] = {"2019E7028", "2019E7029", "2019E7030", "2020E7028", "2020E7029", "2020E7030", "2021E7028", "2021E7030", "2022E7028", "2022E7030"};
        
        double Req_course[] = {3.5, 4.0, 3.4, 3.7, 4.1, 2.5, 1.9, 2.4, 2.7, 3.6};
        double Elective_course[] = {1.9, 3.0, 4.1, 3.7, 2.0, 2.6, 3.7, 2.4, 3.2, 3.8};
        double Liberal_arts[] = {4.0, 2.5, 3.4, 2.0, 3.8, 2.5, 1.7, 4.3, 4.5, 3.0};
        
        double total_score[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        double avg_score[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        Character grade[] = {'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A'};
        int grade_level[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        
        for(int i=0; i<10; i++){
            total_score[i] = Req_course[i] + Elective_course[i] + Liberal_arts[i];
            avg_score[i] = total_score[i] / 3;
            if(avg_score[i] >= 4.0 ){
                grade[i] = 'A';
            } else if(avg_score[i] >= 3.0){
                grade[i] = 'B';
            } else if(avg_score[i] >= 2.0){
                grade[i] = 'C';
            } else if(avg_score[i] >= 1.0){
                grade[i] = 'D';
            } else {
                grade[i] = 'F';
            }
        }
        
        for(int i=0; i<10; i++){
            for(int j=0; j<10; j++){
                if(Req_course[j] < Req_course[i]){
                    double temp1 = Req_course[i];
                    Req_course[i] = Req_course[j];
                    Req_course[j] = temp1;
                    
                    double temp2 = Elective_course[i];
                    Elective_course[i] = Elective_course[j];
                    Elective_course[j] = temp2;
                    
                    double temp3 = Liberal_arts[i];
                    Liberal_arts[i] = Liberal_arts[j];
                    Liberal_arts[j] = temp3;
                    
                    String temp_str1 = Name[i];
                    Name[i] = Name[j];
                    Name[j] = temp_str1;
                    
                    String temp_str2 = SID[i];
                    SID[i] = SID[j];
                    SID[j] = temp_str2;
                    
                    double temp4 = total_score[i];
                    total_score[i] = total_score[j];
                    total_score[j] = temp4;
                    
                    double temp5 = avg_score[i];
                    avg_score[i] = avg_score[j];
                    avg_score[j] = temp5;
                    
                    Character temp_str3 = grade[i];
                    grade[i] = grade[j];
                    grade[j] = temp_str3;
                }
            }
        }
        
        System.out.println("석차 이름    학번   전필 전선 교필 합계 평균 등급");
        for(int i=0; i<10; i++){
            System.out.println(grade_level[i] + "    " +Name[i] + " " + SID[i] + " " + Req_course[i] + " " + Elective_course[i] + " " + Liberal_arts[i] + " " + Math.round(total_score[i]) + " " + Math.round(avg_score[i]) + " " + Math.round(grade[i]) + " ");
        }

        System.out.println("2019E7028 김승종");
    }
}

댓글