본문 바로가기

프로그래밍 언어/Java

[수치계산] 학생 점수 프로그램

학생 점수 프로그램



<입력>


이름 / 학년 / 점수 여러개 (0 입력 시 종료)



<출력>


이름(학년) [최대, 최소 제외한 평균] 점수 나열 (최소 점수, 최대 점수 제외 개수)

1등 : 이름 (최대, 최소 제외한 평균으로 비교)

우수 학생 : 몇명 - 이름, 이름 (최대 45점 이상, 최소 10점 이상인 학생)




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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.Scanner;
import java.util.ArrayList;
 
public class StudentDemo_1 {
    Scanner scan = new Scanner(System.in);
    Student_1[] stList = new Student_1[20];
    int cnt = 0;
    void doit() {
        Student_1 s = null;
        while (true) {
            s = new Student_1(); 
            s.scoreCnt = s.read(scan);
            if(s.scoreCnt==0break;
            s.compute();
            s.compare();
            stList[cnt++= s;
        }
        for (int i=0; i<cnt; i++
            stList[i].print();
        stList[cnt-1].print_end();
    }
    public static void main(String[] args) {
        StudentDemo_1 demo = new StudentDemo_1();
        demo.doit();
    }
}
 
class Student_1 {
    int scoreCnt=0, max=0, min =0;
    String id=null, grade=null, temp_str=null;
    int[] score = new int[20];
    double avg;
    static String bestId=null;
    static int bestInt=0;
    static ArrayList<String> mArrayList = new ArrayList<String>();
    
    int read(Scanner scan) {
        int cnt=0;
        id = scan.next();
        if (id.equals("end")) { return 0; }
        grade = scan.next();
        while (true) {
            score[cnt++= scan.nextInt();
            if (score[cnt-1]==0) { break; }
            continue;
        }
        return cnt;
    }
    
    void compute() {
        double sum=0.0;
        int tmpMax=score[0], tmpMin=score[0];
        for (int i=0; i<scoreCnt-1; i++) {
            if (tmpMax<score[i]) tmpMax=score[i];
            if (tmpMin>score[i]) tmpMin=score[i];
            max = tmpMax; min = tmpMin;
        }
        if(max>=45 && min>=10) { mArrayList.add(id); }
        for (int i=0; i<scoreCnt-1; i++) {
            if (score[i]==max || score[i]==min) { continue; }
            else { sum += score[i]; }
        }
        avg = sum / (scoreCnt-3);
    }
    
    void compare() {
        if(bestInt < avg) { bestId = id; }
    }
    
    void print() {
        int tmp=0;
        System.out.printf("%-4s(%s%s) [%-4.2f] ", id, grade, "학년", avg);
        for (int i=0; i<scoreCnt-1; i++) {
            System.out.printf("%-4d", score[i]);
        }
        tmp = scoreCnt-3;
        System.out.println(" (" + min + ", " + max + " 제외 " + tmp + "개)");
    }
    
    void print_end() {
        System.out.println("1등 : " + bestId);
        System.out.print("우수선수 : " + mArrayList.size() + "명 -");
        for (int i=0; i<mArrayList.size(); i++
            System.out.print(" " + mArrayList.get(i));
        System.out.println();
    }
}
cs