반응형
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package scoreProcessingProgram;
 
import java.util.Scanner;
 
public class ScoreProcessingSystem {
 
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
 
        // 매크로 설정
        int NAME = 0;
        int KOR = 1;
        int ENG = 2;
        int MATH = 3;
        int SUM = 4;
        int AVG = 5;
 
        int stuDataNum = 6// 한 학생에 대한 정보 수
        int numOfStu = 0// 총 학생 수
        String tmp; // 입력받을 때 임의로 쓰는 변수
        char isContinue; // 프로그램 진행 여부 변수
 
        boolean isExistStudent = false// 학생을 검색할 때 존재 여부 확인
        boolean isExistScore = false// 검색하고 싶은 성적이 존재하는지
        boolean isSuccessful = false// 성적 수정 여부 결정
 
        int stuIndex = 0// 입력받을 위치
        int stuCount = 0// 입력받은 학생 수
        int deleteCount = 0//몇명의 학생이 삭제되었는지
 
        // 한 학생에 대한 정보
        final String[] stuData = new String[stuDataNum]; // {'이름', '국어', '영어', '수학', '총점', '평균'}
        stuData[0= new String("이름");
        stuData[1= new String("국어");
        stuData[2= new String("영어");
        stuData[3= new String("수학");
        stuData[4= new String("총점");
        stuData[5= new String("평균");
 
        String[][] stu;
 
        System.out.println("학생 수 입력 : ");
        numOfStu = sc.nextInt();
 
        stu = new String[numOfStu][];
        do {
 
            // 기능 구현
            System.out.print("1.입력  2.출력  3.검색  4.수정  5.삭제 : ");
            int selectedFunction = sc.nextInt(); // user가 원하는 기능 입력 받는 변수
 
            switch (selectedFunction) {
            case 1// 1. 입력
                stu[stuIndex] = new String[stuDataNum];
 
                System.out.println((stuIndex + 1+ "번째 학생의 정보 입력");
 
                for (int i = 0; i < stuDataNum; i++) {
                    if (i == SUM)
                        break// sum과 avg는 입력받을 필요 없음
 
                    System.out.print(stuData[i] + " : ");
                    tmp = sc.next();
                    stu[stuIndex][i] = new String(tmp);
                }
 
                // sum, avg계산
                stu[stuIndex][SUM] = Integer.toString(Integer.parseInt(stu[stuIndex][KOR])
                        + Integer.parseInt(stu[stuIndex][ENG]) + Integer.parseInt(stu[stuIndex][MATH]));
                stu[stuIndex][AVG] = Integer.toString(Integer.parseInt(stu[stuIndex][SUM]) / 3);
 
                stuIndex++;
                stuCount++;
 
                break;
            case 2// 2. 출력
                // if(stu[i][NAME].equals("")) { count--;}
 
                // 정보가 있는지 확인
                if (numOfStu == 0 || stuCount == 0) {
                    System.out.println("학생 정보가 없습니다.");
                    break;
                }
 
                System.out.println();
                System.out.println("--학생 정보 출력--");
 
                for (int i = 0; i < stuCount + deleteCount; i++) {
 
                    for (int j = 0; j < stuDataNum; j++) {
 
                        if (!stu[i][j].equals("null"))
                            System.out.print(stu[i][j] + " ");
                    }
                    System.out.println();
                }
                System.out.println();
 
                break;
            case 3// 3. 검색
                if (stuCount == 0) {
                    System.out.println("학생 정보가 없습니다.");
                    break;
                }
 
                System.out.print("검색하고 싶은 성적을 입력하세요 : ");
                int userWantToSearch = sc.nextInt();
 
                System.out.println("--학생 정보 출력--");
 
                for (int i = 0; i < stuIndex; i++) {
                    if (stu[i][SUM].equals("null"))
                        continue;
 
                    if (Integer.parseInt(stu[i][SUM]) >= userWantToSearch) {
 
                        // 조건에 만족하는 결과 값 출력
                        for (int j = 0; j < stu[i].length; j++) {
                            System.out.print(stu[i][j] + " ");
                        }
                        isExistScore = true;
                        System.out.println();
                    }
                }
                if (isExistScore == false) {
                    System.out.println("조건에 만족하는 성적이 없습니다.");
                }
                break;
            case 4// 4. 수정
 
                if (stuCount == 0) {
                    System.out.println("학생 정보가 없습니다.");
                    break;
                }
                System.out.print("수정하고 싶은 학생의 이름을 입력하세요 : ");
                String name = sc.next(); // 수정할 학생 이름을 저장하는 변수
 
                // 검색할 학생 존재 여부
                for (int i = 0; i < stu.length; i++) {
                    if (name.equals(stu[i][NAME])) {
                        isExistStudent = true;
                        break;
                    }
                }
                if (isExistStudent) { // 검색한 학생 존재
                    System.out.print(name + " 학생의 어떤 성적을 수정하시겠어요? 1.국어 2.영어 3.수학");
                    int userWanToChangeScore = sc.nextInt(); // 수정하고싶은 과목
 
                    int changeScore; // 수정하고 싶은 성적 입력 받을 변수
 
                    switch (userWanToChangeScore) {
                    case 1:
                        System.out.print(stuData[KOR] + " 성적 몇점으로 수정하시겠어요? ");
                        changeScore = sc.nextInt();
 
                        for (int i = 0; i < stuIndex; i++) {
                            if (stu[i][NAME].equals(name)) {
                                stu[i][KOR] = Integer.toString(changeScore);
                            }
                        }
                        isSuccessful = true;
                        break;
 
                    case 2:
                        System.out.print(stuData[ENG] + " 성적 몇점으로 수정하시겠어요? ");
                        changeScore = sc.nextInt();
 
                        for (int i = 0; i < stuIndex; i++) {
                            if (stu[i][NAME].equals(name)) {
                                stu[i][ENG] = Integer.toString(changeScore);
                            }
                        }
                        isSuccessful = true;
                        break;
 
                    case 3:
                        System.out.print(stuData[MATH] + " 성적 몇점으로 수정하시겠어요? ");
                        changeScore = sc.nextInt();
 
                        for (int i = 0; i < stuIndex; i++) {
                            if (stu[i][NAME].equals(name)) {
                                stu[i][MATH] = Integer.toString(changeScore);
                            }
                        }
                        isSuccessful = true;
                        break;
                    default:
                        System.out.println("잘못 눌렀습니다.");
 
                    }
                    if (isSuccessful) {
                        // 총점, 평점 수정
                        for (int i = 0; i < stuIndex; i++) {
                            if (stu[i][NAME].equals(name)) {
                                stu[i][SUM] = Integer.toString(Integer.parseInt(stu[i][KOR])
                                        + Integer.parseInt(stu[i][ENG]) + Integer.parseInt(stu[i][MATH]));
                                stu[i][AVG] = Integer.toString(Integer.parseInt(stu[i][SUM]) / 3);
                            }
                        }
                        System.out.println("수정이 완료되었습니다.");
                    }
                    
                }
                break;
            case 5// 5. 삭제
                System.out.print("삭제할 학생의 이름을 입력하세요 : ");
                String userWantToDeleteData = sc.next();
 
                for (int i = 0; i < stuIndex; i++) {
                    if (userWantToDeleteData.equals(stu[i][NAME])) {
                        isExistStudent = true;
 
                        for (int j = 0; j < stuDataNum; j++) {
                            stu[i][j] = "null";
                        }
                        stuCount--;
                        
                    }
                }
 
                if (!isExistStudent) {
                    System.out.println("검색하신 학생이 없습니다.");
                    break;
                }
                System.out.println("삭제가 완료되었습니다.");
                isExistStudent = false;
                deleteCount++;
                break;
            default:
                System.out.println("잘못 입력했습니다.");
            }
 
            System.out.println("끝내려면 n을 누르세요");
            isContinue = sc.next().charAt(0);
        } while (isContinue == 'n' || stuCount == 5);
 
        System.out.println("끝.");
 
    }
 
}
 
cs


반응형

'Programming Language > JAVA' 카테고리의 다른 글

[급여관리 프로그램(Has~A 만)  (24) 2018.07.16
[Java]Enum  (0) 2018.07.12
클래스 구성요소  (0) 2018.07.10
이클립스 단축키 모음  (0) 2018.07.05
<Java> 제네릭  (0) 2018.01.08

+ Recent posts