[JAVA] Day_02. 성적관리프로그램
JAVA 2018. 3. 21. 10:40 |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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | package Day_02; import java.util.Scanner; class Name { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } class Score { private int kor, eng, math; public void setKor(int kor) { this.kor = kor; } public int getKor() { return kor; } public void setEng(int eng) { this.eng = eng; } public int getEng() { return eng; } public void setMath(int math) { this.math = math; } public int getMath() { return math; } } class Student { Name name = new Name(); Score score = new Score(); public int getSum() { return score.getKor()+score.getEng()+score.getMath(); } public double getAvg() { return this.getSum() / 3.0; } public String printAll() { return " 이름: "+name.getName()+ " 국어: "+score.getKor()+ " 영어: "+score.getEng()+ " 수학: "+score.getMath()+ " 총점: "+this.getSum()+ " 평균: "+this.getAvg()+"\n"; } } public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Student[] st = new Student[50]; // 50명을 동적할당받음 String who; int i=0, j; int num=0; while(num<=6) { System.out.print("1.입력 2.전체출력 3.검색 4.수정 5.삭제 6.종료 : "); num = sc.nextInt(); switch(num) { case 1: // 1. 입력 (name, kor, mat, eng) st[i] = new Student(); System.out.print("이름 입력 : "); st[i].name.setName(sc.next()); System.out.print("국어점수 입력 : "); st[i].score.setKor(sc.nextInt()); System.out.print("영어점수 입력 : "); st[i].score.setEng(sc.nextInt()); System.out.print("수학점수 입력 : "); st[i].score.setMath(sc.nextInt()); i++; break; case 2: // 2. 전체출력 (오름차순으로 정렬해서 총점, 평균을 출력) Student temp = new Student(); for(j=0; j<st.length; j++) { if(st[j]==null) break; } // 버블정렬 for(int a=0; a<j-1; a++) { for(int b=0; b<j-1; b++) { if(st[b].getSum() < st[b+1].getSum()) // 작은거부터 차례대로 정렬 { temp = st[b]; st[b] = st[b+1]; st[b+1] = temp; } } } for(int p=0; p<j; p++) { System.out.println(st[p].printAll()); } break; case 3: // 3. 검색 (이름을 입력하면 성적순으로 출력) System.out.print("검색할 사람의 이름 : "); who = sc.next(); for(j=0; j<st.length; j++) { if(st[j].name.getName().equals(who)) { break; } } System.out.println(st[j].printAll()); break; case 4: // 4. 수정 System.out.print("수정할 사람의 이름 : "); who = sc.next(); for(j=0; j<st.length; j++) { if(st[j].name.getName().equals(who)) break; } System.out.print("수정할 내용은? 1.국어 2.영어 3.수학 : "); int k = sc.nextInt(); if(k==1) { System.out.print("국어점수 수정 : "); st[j].score.setKor(sc.nextInt()); } else if(k==2) { System.out.print("영어점수 수정 : "); st[j].score.setEng(sc.nextInt()); } else if(k==3) { System.out.print("수학점수 수정 : "); st[j].score.setMath(sc.nextInt()); } else System.out.println("잘못된 입력입니다."); break; case 5: // 5. 삭제 System.out.print("삭제할 사람의 이름 : "); who = sc.next(); for(j=0; j<st.length; j++) { if(st[j].name.getName().equals(who)) { break; } } st[j] = null; // 지운다 // st.length-1-j for(int shift=j; shift<st.length-1; shift++) { st[shift] = st[shift+1]; } st[st.length-1] = null; break; case 6: // 6.종료 System.out.println("종료!!"); break; } } sc.close(); } } | cs |
'JAVA' 카테고리의 다른 글
내부 클래스 2 (0) | 2018.03.22 |
---|---|
내부 클래스 (0) | 2018.03.22 |
[JAVA] Day_03. Dynamic Binding : Stack & Queue (0) | 2018.03.21 |
[JAVA] Day_01. +/- 계산기 (0) | 2018.03.20 |
사칙연산 (0) | 2018.03.19 |