-
(Java)자바로 학점계산기 만들기개발 완료 2020. 3. 12. 16:27반응형
1. 목표
- If ~ else 구문을 이용해서 간단한 학점계산기 만들기
- 코드를 줄여서 효율화
2. 전체 코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersimport java.util.Scanner; public class study { public static void main(String[] args) { int score; int opt = 0; char grade; System.out.println("Please enter your score: "); Scanner scanner = new Scanner(System.in); score = scanner.nextInt(); if (score >= 90) { grade = 'A'; if (score >= 98) { opt = '+'; } else if (score < 94) { opt = '-'; } } else if (score >= 80) { grade = 'B'; if (score >= 88) { opt = '+'; } else if (score < 84) { opt = '-'; } } else { grade = 'c'; } System.out.println("Your score: " + score + "\nYour grade: " + grade + "" + opt ); } } 3. 상세 코드 설명
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersint score; int opt = 0; char grade; - score, opt, grade 변수 생성
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersSystem.out.println("Please enter your score: "); Scanner scanner = new Scanner(System.in); score = scanner.nextInt(); - Scanner 클래스를 이용해서, client가 입력한 값을 받습니다
- scanner.nextInt를 이용해서 콘솔에서 값을 읽어온 후 score 변수에 저장합니다
- score 변수 1개이므로 값을 2개 이상 입력해도, 1개만 저장 가능합니다
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersif (score >= 90) { grade = 'A'; if (score >= 98) { opt = '+'; } else if (score < 94) { opt = '-'; } } else if (score >= 80) { grade = 'B'; if (score >= 88) { opt = '+'; } else if (score < 84) { opt = '-'; } } else { grade = 'c'; } - 90점 이상 --> 기본 A학점 (grade)
- 98점 이상 --> A + '+' (opt)
- 90 ~ 93점 --> A + '-' (opt)
- 94 ~ 97점 --> A + '0' (opt)
- 80 ~ 89점 --> else if문 시작
- ~ 79점 --> else문 실행
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersSystem.out.println("Your score: " + score + "\nYour grade: " + grade + "" + opt ); - 변수값에 저장된 값을 출력
- "\n" 이용해서 score, grade 개행
결과값 4. 코드 줄이기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersimport java.util.Scanner; public class study { public static void main(String[] args) { int score; char opt = '0'; char grade = 'c'; System.out.println("Please enter your score: "); Scanner scanner = new Scanner(System.in); score = scanner.nextInt(); if (score >= 90) { grade = 'A'; if (score >= 98) { opt = '+'; } else if (score < 94) { opt = '-'; } } else if (score >= 80) { grade = 'B'; if (score >= 88) { opt = '+'; } else if (score < 84) { opt = '-'; } } // else { // grade = 'c'; // } System.out.println("Your score: " + score + "\nYour grade: " + grade + "" + opt ); } } - char grade 변수에 'c' 기본값 입력
- else문 주석처리. 어차피 해당 안되면 다 C로 처리하기 때문에 굳이 필요X
- ~ 79점까지는 전부 'c'학점 처리
결과값 참고로 이 코드들은 아주 간단하고 하드코딩으로 무식하게 만든 코드입니다. 여기서 발전을 시킨다면 좀 더 세련되고 코드수를 줄이고, 객체지향적인 코도로 수정할 수 있습니다. 이 코드는 초보자들이 간단히 개념을 보는 정도로 활용하시기를 추천드립니다ㅎㅎ
5. 수정 사항
2021년 1월10일
설명 및 코드 참조 방식 변경
반응형'개발 완료' 카테고리의 다른 글
Java 특정배수가 아닌 값의 총합 (0) 2020.03.17 Java 다양한 구구단 만들기 (0) 2020.03.17 (for문)자바로 별 찍기 (0) 2020.03.16 Java - Switch문으로 계절 나타내기 (0) 2020.03.13 Java - 원하는 숫자를 입력해 구구단 만들기 (0) 2019.12.17