JAVA/code by me
Score Program - ArrayList, while 과 do while, while안에서의 if와 switch 그리고 break;
Y_____527
2021. 1. 29. 00:17
<Score.java>
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
|
package quiz2.ex01.score;
//수정하기를 구체적으로 나누기
public class Score_2 {
protected String name;
protected int kor, eng, com, tot;
protected double avg;
protected char grade;
// 생성자
public Score_2() {
}
public Score_2(String name, int kor, int eng, int com, int tot, double avg, char grade) {
super();
this.name = name;
this.kor = kor;
this.eng = eng;
this.com = com;
this.tot = tot;
this.avg = avg;
this.grade = grade;
}
public Score_2(String name, int kor, int eng, int com) {
this.name = name;
this.kor = kor;
this.eng = eng;
this.com = com;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getCom() {
return com;
}
public void setCom(int com) {
this.com = com;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public double getAvg() {
return avg;
}
public void setAvg(double avg) {
this.avg = avg;
}
public char getGrade() {
return grade;
}
public void setGrade(char grade) {
this.grade = grade;
}
// output method
public void display(int num) {
System.out.println("\n\n****** " + (num + 1) + "번학생 " + name + " 님의 성적표 ******");
System.out.println("Kor : " + kor + "\tEng : " + eng + "\tCom : " + com);
System.out.print("Total : " + tot);
System.out.printf("\tAverage : %.2f\tGrade : %c", avg, grade);
}
}
|
cs |
<ScoreManager.java>
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
|
package quiz2.ex01.score;
import java.util.ArrayList;
public class ScoreManager_2 extends Score_2{
ArrayList<Score_2> list = new ArrayList<Score_2>();
public ScoreManager_2() {
}
public ScoreManager_2(String name, int kor, int eng, int com, int tot, double avg, char grade) {
Score_2 s = new Score_2(name, kor, eng, com, tot, avg, grade);
list.add(s);
}
//추가
public void ScoreAdd(String name, int kor, int eng, int com, int tot, double avg, char grade) {
Score_2 s = new Score_2(name, kor, eng, com, tot, avg, grade);
list.add(s);
}
//삭제
public void ScoreRmv(int i) {
System.out.println("\n\n* 삭제된 학생");
list.get(i-1).display(i);
System.out.println("--------------");
list.remove(i-1);
}
//수정->각각 수정하려면 필요없..음(main에서 실행가능)
// public void ScoreCh(int i, String name, int kor, int eng, int com) {
// Score_2 s = new Score_2(name, kor, eng, com);
// list.set(i-1, s);
// }
//출력
public void display(){
System.out.println("저장된 학생 수 : " + list.size());
for(int i=0; i<list.size(); i++){
System.out.println();
list.get(i).display(i);
}// for
}// display()
}// ScoreManaget_2 end
|
cs |
<Main.java>
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
|
package quiz2.ex01.score;
//수정하기를 구체적으로 나누기
import java.util.Scanner;
public class MainEntry_2 {
static String name;
static int kor;
static int eng;
static int com;
static int tot;
static double avg;
static char grade;
public static void main(String[] args) {
ScoreManager_2 sm = new ScoreManager_2();
Scanner sc = new Scanner(System.in);
int edit_num;
try {
while (true) {
System.out.println("\n\n*-*-*-*-* 메뉴 *-*-*-*-*");
System.out.println("1. 학생 정보 입력");
System.out.println("2. 학생 정보 삭제");
System.out.println("3. 학생 정보 수정");
System.out.println("4. 모든 학생 보기");
System.out.println("5. 종료");
System.out.println("----------------------");
System.out.print(" >> ");
int inNum = sc.nextInt();
try {
switch (inNum) {
case 1:
System.out.println("\n*-*-*-* 1. 학생 정보 입력 *-*-*-*");
inScore();
sm.ScoreAdd(name, kor, eng, com, tot, avg, grade);
System.out.println("* 입력완료");
break;
case 2:
if(sm.list.isEmpty()) {
System.out.println("삭제할 학생이 없습니다.");
continue;
}
System.out.println("\n*-*-*-* 2. 학생 정보 삭제 *-*-*-*");
sm.display();
System.out.println("---------------------------");
System.out.print("삭제할 학생의 번호를 입력하세요 >> ");
inNum = sc.nextInt();
sm.ScoreRmv(inNum);
System.out.print("삭제되었습니다.");
break;
case 3:
if(sm.list.isEmpty()) {
System.out.println("수정할 학생이 없습니다.");
continue;
}
System.out.println("\n*-*-*-* 3. 학생 정보 수정 *-*-*-*");
sm.display();
System.out.println("---------------------------");
System.out.print("수정할 학생의 번호를 입력하세요 >> ");
inNum = sc.nextInt();
// do { //=>1)do ~ while 이용
while (true) { //=>2)while 이용
System.out.println("수정할 학생의 정보를 입력하세요.(1.이름, 2.국어점수, 3.영어점수, 4.전산점수)");
edit_num = sc.nextInt();
switch (edit_num) {
case 1: // 이름수정
// editName();
System.out.println("수정할 이름을 입력해주세요=>");
sm.list.get(inNum - 1).setName(sc.next());
System.out.println("수정되었습니다.");
// sm.ScoreCh(inNum, name, kor, eng, com);
break;
case 2: // 국어점수 수정
System.out.println("국어점수를 입력해주세요=>");
sm.list.get(inNum - 1).setKor(sc.nextInt());
System.out.println("수정되었습니다.");
break;
case 3: // 영어점수 수정
System.out.println("영어점수를 입력해주세요=>");
sm.list.get(inNum - 1).setEng(sc.nextInt());
System.out.println("수정되었습니다.");
break;
case 4: // 전산점수 수정
System.out.println("전산점수를 입력해주세요=>");
sm.list.get(inNum - 1).setEng(sc.nextInt());
System.out.println("수정되었습니다.");
break;
default:
System.out.println("수정할 정보의 번호를 다시 입력해주세요.");
continue;
} // in switch end
System.out.println("계속 수정하시겠습니까?");
String con = sc.next();
if (con.equalsIgnoreCase("n")) {
break;
}
} //in while end
// } while (edit_num != 1 && edit_num != 2 && edit_num != 3 && edit_num != 4); // do while end
break;
case 4:
if(sm.list.isEmpty()) {
System.out.println("저장된 학생정보가 없습니다.");
continue;
}
System.out.println("\n*-*-*-* 4. 모든 학생 보기 *-*-*-*");
sm.display();
break;
case 5:
System.out.println("\n* 프로그램을 종료합니다.");
System.exit(0);
break;
default:
System.out.println("\nerr)잘못입력하셨습니다. 다시 입력해주세요.");
} // out switch end
} catch (Exception e) {
System.out.println("err) 없는 학생입니다. 다시 입력해주세요.");
} // try catch
} // out while end
} catch (Exception e) {
System.out.println("err) 잘못 입력하셨습니다. 프로그램을 종료합니다.");
} // try catch
}
public static void inScore() {
Scanner sc = new Scanner(System.in);
System.out.print("학생 이름을 입력해주세요: ");
name = sc.next();
do {
System.out.println("학생의 국어점수를 입력하세요(0~100점사이값만 넣으세요) : ");
kor = sc.nextInt();
} while ((kor < 0) || (kor > 100));
do {
System.out.println("학생의 영어점수를 입력하세요(0~100점사이값만 넣으세요) : ");
eng = sc.nextInt();
} while ((eng < 0) || (eng > 100));
do {
System.out.println("학생의 전산점수를 입력하세요(0~100점사이값만 넣으세요) : ");
com = sc.nextInt();
} while ((com < 0) || (com > 100));
process(name, kor, eng, com);
}
// 계산처리
public static void process(String name, int kor, int eng, int com) {
tot = kor + eng + com;
avg = tot / 3.0;
grade(avg);
} // process end
public static void grade(double avg) {
if (avg <= 100 && avg > 90) {
grade = 'A';
} else if (avg <= 90 && avg > 80) {
grade = 'B';
} else if (avg <= 80 && avg > 70) {
grade = 'C';
} else if (avg <= 70 && avg > 60) {
grade = 'D';
} else
grade = 'F';
} // grade end
}
|
cs |
▶try ~ catch 문에서 catch는 여러개 쓸 수 있음
▶main메소드가 있는 클래스 안의 다른 메소드와 변수들은 접근지정자가 모두 main과 같은 static이여야만 함
->그래야 main메소드 안에서 불러오고 사용가능
▶while(true)와 do ~ while 모두 사용해봄
->do ~ while은 while(조건)에서 조건에 맞는 상황에는 계속 실행: 여러 조건 넣을 때 ||와 && 혼동하지 않기
무한루프 안의 if문 안에서의 break; | 무한루프 안의 switch문 안에서의 break; |
while(true) { if문 ~~ { break; }} |
while(true) { switch문~{ case 1 : break; }} ==>switch문 탈출, while이 다시 반복 실행 |