본문 바로가기
22.05.24~22.11.16/Project

6월 project 1 <생성자 선언, 메소드 호출, Scanner 사용해서 학생 점수표 만들기>

by 10월의끝 2022. 6. 3.

<생성자 선언과 메소드 호출>

 

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
public class Student {
    String name;
    int kor;
    int math;
    int eng;
    
    Student(String name) {   //생성자
        this.name = name;
    }
    
    Student(String name, int kor, int math, int eng) {
        this.name = name;
        this.kor = kor;
        this.math = math;
        this.eng = eng;        
    }
    
    String content() {
        return name + "," + kor + "," + math + "," + eng;
    }
    
    int sum() {
        return kor+math+eng;
    }
    
    float avg() {
        return (float) sum() / 3;
    }
}
 
cs

 

<스캐너 활용> 

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
import java.util.Scanner;
 
public class StudentTest {
    public static void main(String[] args) {
        boolean run = true;
        int studentNum = 0;
        Student[] stu = null;
        
        Scanner scan = new Scanner(System.in);
        while (run) {
            System.out.println("----------------------------------------------------------------");
            System.out.println("1. 학생등록  | 2. 점수리스트 | 3. 총점과 평균  | 4.분석  | 5. 종료");
            System.out.println("----------------------------------------------------------------");
            System.out.print("선택> ");
            int selectNo = scan.nextInt();
            switch(selectNo) {
            case 1:
                System.out.print("학생수> ");
                studentNum = scan.nextInt();
                stu = new Student[studentNum];
                
                for (int i=0; i<studentNum; i++) {
                    scan.nextLine();
                    System.out.print(i+1 + "번 학생이름> ");
                    String name = scan.nextLine();
                    System.out.print("국어 점수> ");
                    int kor = scan.nextInt();
                    System.out.print("수학 점수> ");
                    int math = scan.nextInt();
                    System.out.print("영어 점수> ");
                    int eng = scan.nextInt();
                    stu[i] = new Student(name, kor, math, eng);    
                }
                break;
            case 2:
                for (Student st : stu) {
                    System.out.println(st.content());
                }
                break;
                    
            case 3:
                for (Student st : stu) {
                    System.out.println("총점:"+ st.sum() +" 평균:" + st.avg());
                }
                break;
            
            case 4:
                int kmax = 0;
                int mmax = 0;
                int emax = 0;
                int sum = 0;
                double avg = 0;
                for (Student st : stu) {
                    kmax = (kmax < st.kor)? st.kor : kmax;
                    mmax = (mmax < st.math)? st.math : mmax;
                    emax = (emax < st.eng)? st.eng : emax;
                    sum += st.avg();
                }
                avg = (double) sum / studentNum;
                    System.out.println("국어 최고 점수: " + kmax); 
                    System.out.println("수학 최고 점수: " + mmax);
                    System.out.println("영어 최고 점수: " + emax);
                    System.out.println("평균 점수: " + avg);
                    break;
                
            case 5:
            run = false;
            System.out.println("프로그램 종료");
        }
      }
    }
}
 
cs

 

실행 결과

------------------------------------------------------
1. 학생등록  | 2. 점수리스트 | 3. 총점과 평균  | 4.분석  | 5. 종료
------------------------------------------------------
선택> 1
학생수> 2
1번 학생이름> 김자바
국어 점수> 78
수학 점수> 98
영어 점수> 76
2번 학생이름> 박코딩
국어 점수> 85
수학 점수> 74
영어 점수> 96
------------------------------------------------------
1. 학생등록  | 2. 점수리스트 | 3. 총점과 평균  | 4.분석  | 5. 종료
------------------------------------------------------
선택> 2
김자바,78,98,76
박코딩,85,74,96
------------------------------------------------------
1. 학생등록  | 2. 점수리스트 | 3. 총점과 평균  | 4.분석  | 5. 종료
------------------------------------------------------
선택> 3
총점:252 평균:84.0
총점:255 평균:85.0
------------------------------------------------------
1. 학생등록  | 2. 점수리스트 | 3. 총점과 평균  | 4.분석  | 5. 종료
------------------------------------------------------
선택> 4
국어 최고 점수: 85
수학 최고 점수: 98
영어 최고 점수: 96
평균 점수: 84.5
------------------------------------------------------
1. 학생등록  | 2. 점수리스트 | 3. 총점과 평균  | 4.분석  | 5. 종료
------------------------------------------------------
선택> 5
프로그램 종료