문제풀이/Java
[Java] 학점 구하기
by AngieLee
2021. 6. 3.
import java.util.*;
class Grade {
Scanner scr = new Scanner(System.in);
String[] sub = {"국어", "영어", "수학", "과학"};
String scoreStr;
int i = 0;
int score = 0;
int sum = 0;
int avg = 0;
char gra;
void input() {
while(i<sub.length) {
System.out.print(sub[i]+": ");
scoreStr = scr.nextLine();
try {
score = Integer.parseInt(scoreStr);
}catch(Exception e){
System.out.println("숫자로만 입력하세요!");
continue;
}
if(score<0 || score>100) {
System.out.println("0~100까지 점수만 입력하세요!");
continue;
}
i++;
sum = sum+score;
avg = sum/sub.length;
}
}
void output() {
gra = (avg >= 90) ? 'A' :
(avg >= 80) ? 'B' :
(avg >= 70) ? 'C' :
(avg >= 60) ? 'D' : 'F';
System.out.println("학점: "+gra+" (총점: "+sum+"점, 평균: "+avg+"점)");
}
public static void main(String[] args) {
System.out.println("점수를 입력해주세요!");
Grade g = new Grade();
g.input();
g.output();
}
}