이번주는 변수,조건문과 같은 기본 문법에 대해서 공부했다.
파이썬에 비해 길고, 아직 낯설어서 어떻게 효율적으로 바꿔야하는지 잘 모르겠다.
코드를 작성하기는 했는데 너무 길고 중복되는 부분이 많아 비효율적인 것 같다.
조금 더 효율적이게 바꿀 부분이 보이는 것 같은데 방법을 아직 잘 몰라서 이게 최선인 듯..!
방법이 있을까 ?
미션 문제
합격입니다.라는 문구를 출력해야 한다.불합격입니다.라는 문구를 출력해야 한다.import java.util.*;
public class Main {
public static void main(String[] args) {
int maxScore = 0;
int minScore = 100;
int group,scoreHTML,scoreCSS,scoreJS;
double avgScore;
Scanner input = new Scanner(System.in);
System.out.println("몇 기인지 입력해주세요.");
group = input.nextInt();
System.out.println("HTML 과목 점수를 입력해주세요.");
scoreHTML = input.nextInt();
System.out.println("CSS 과목 점수를 입력해주세요.");
scoreCSS = input.nextInt();
System.out.println("Javascript 과목 점수를 입력해주세요.");
scoreJS = input.nextInt();
avgScore = (scoreJS + scoreCSS + scoreHTML) / 3.0;
if (scoreJS == 100 && scoreCSS == 100){
System.out.println("합격입니다.\n" +
"전체 과목 중 최고점은 100점입니다.\n" +
"전체 과목 중 최저점은 100점입니다.\n" +"전체 과목의 평균은 " +avgScore+"점입니다.");
}
else if (scoreHTML == 100 && scoreCSS == 100){
System.out.println("합격입니다.\n" +
"전체 과목 중 최고점은 100점입니다.\n" +
"전체 과목 중 최저점은 100점입니다.\n" +"전체 과목의 평균은 " +avgScore+"점입니다.");
}
else if (scoreHTML == 100 && scoreJS == 100){
System.out.println("합격입니다.\n" +
"전체 과목 중 최고점은 100점입니다.\n" +
"전체 과목 중 최저점은 100점입니다.\n" +"전체 과목의 평균은 " +avgScore+"점입니다.");
}
else{
if(scoreHTML > scoreJS && scoreHTML > scoreCSS){
maxScore = scoreHTML;
if(scoreJS > scoreCSS){
minScore = scoreCSS;
}
else{
minScore = scoreJS;
}
}
else if(scoreJS > scoreHTML && scoreJS > scoreCSS){
maxScore = scoreJS;
if(scoreHTML > scoreCSS){
minScore = scoreCSS;
}
else{
minScore = scoreHTML;
}
}else if(scoreCSS > scoreHTML && scoreCSS > scoreJS){
maxScore = scoreCSS;
if(scoreHTML > scoreJS){
minScore = scoreJS;
}
else{
minScore = scoreHTML;
}
}
if (group == 1 || group == 2){
if ( avgScore >= 60){
System.out.println("합격입니다.");
}
else{
System.out.println("불합격입니다.");
}
}
else if (group == 3){
if (avgScore >= 70){
System.out.println("합격입니다.");
}
else{
System.out.println("불합격입니다.");
}
}
System.out.println("전체 과목 중 최고점은 "+maxScore+"점입니다.");
System.out.println("전체 과목 중 최저점은 "+minScore+"점입니다.");
System.out.println("전체 과목의 평균은 "+avgScore+"점입니다.");
}
}
}
maxScore와 minScore부분을 Math 클래스를 이용해 나타내면 두줄로 나타낼 수 있었다!!
maxScore = Math.max(Math.max(scoreCSS,scoreJS),scoreHTML);
minScore = Math.min(Math.min(scoreCSS,scoreJS),scoreHTML);



미션 잘 수행해주셨네요!! 길고 중복되는 부분을 줄이는 방식은 추후에 클래스와 객체지향을 배우면서 조금씩 나아지게 될거에요:) 지금 현재는 입출력, 변수, 연산자, 형변환, 조건문의 개념을 익힌 것만으로도 충분히 잘 하셨습니다!