Language/JAVA

JAVA 프로그래밍 기초 연습 문제 - 학점 계산기

print(blue) 2024. 4. 1. 11:30

 

https://print-blue.tistory.com/116

 

JAVA 프로그래밍 기초 연습 문제 - 조건문 if문을 활용해 학점 계산기 만들기

점수를 입력받은 후, 학점을 출력하시오. 90~93 : A-, 94~96 : A0, 97~100 : A+ 80~83 : B-, 84~86 : B0, 87~89 : B+ 70~73 : C-, 74~76 : C0, 77~79 : C+ 나머지 : F > 점수를 입력하세요 ... 00 > 학점은 < 00 > 입니다. import java.util

print-blue.tistory.com

 

점수를 입력받은 후, 학점을 출력하시오.
 
90~93 : A-, 94~96 : A0, 97~100 : A+
80~83 : B-, 84~86 : B0, 87~89  : B+
70~73 : C-, 74~76 : C0, 77~79  : C+
나머지 : F

<< 입력 형태 >>
점수를 입력하세요 ... 00

<< 출력 형태 >>
학점은 < 00 > 입니다.

 

grade

0 ~ 69 : F

70 ~ 79 : C

80 ~ 89 : B

90 ~ 100 : A

101 ~ : 잘못된 숫자

 

option

0 ~ 3 : -

4 ~ 6 : 0

7 ~ 9 : +

 

변수는 에러방지를 위해 초기값 설정하기

 

import java.util.Scanner;

public class IfQuiz04Refactoring {

	public static void main(String[] args) {
		
        // 1. 사용자에게 입력받기 위해 scanner 객체 생성
        Scanner input = new Scanner(System.in);
        char grade;
        char option = '0';

        // 2. 사용자에게 입력받은 값을 각 변수에 저장
        System.out.print("점수를 입력하세요 ... ");
        int score = input.nextInt();

        // 3. 조건문 실행 후 출력
        if (score >= 90 && score <= 100) {
            grade = 'A';
            if (score % 10 >= 7 || score == 100) { // A+, A-, A0 구분
                option = '+';
            } else if (score % 10 >= 4) {
                option = '-';
            } else {
                option = '-';
            }

        } else if (score >= 80 && score <= 89) {
            grade = 'B';
            if (score % 10 >= 7) { 
                option = '+';
            } else if (score % 10 >= 4) {
                option = '0';
            } else {
                option = '-';
            }

        } else if (score >= 70 && score <= 79) {
            grade = 'C';
            if (score % 10 >= 7) { 
                option = '+';
            } else if (score % 10 >= 4) {
                option = '0';
            } else {
                option = '-';
            }
        } else {
            grade = 'F';
            option = ' ';
        }

        System.out.println("학점은 " + grade + option + "입니다.");

        // 4. scanner 닫기(오류 방지)
        input.close();
	
	}
}
import java.util.Scanner;

public class IfQuiz04Refactoring {

	public static void main(String[] args) {
		
        // 1. 사용자에게 입력받기 위해 scanner 객체 생성
        Scanner input = new Scanner(System.in);
        char grade = ' '; // 에러방지를 위해 초기값 설정하기
        char option = ' ';

        // 2. 사용자에게 입력받은 값을 각 변수에 저장
        System.out.print("점수를 입력하세요 ... ");
        int score = input.nextInt();

        // 3-1. 조건문에 따라 option 처리
        if (score % 10 >= 7 || score == 100) { 
            option = '+';
        } else if (score % 10 >= 4) {
            option = '0';
        } else {
            option = '-';
        }
        
        // 3-2. 조건문에 따라 grade 처리
        if (score >= 90 && score <= 100) {
            grade = 'A';
        } else if (score >= 80 && score <= 89) {
            grade = 'B';
        } else if (score >= 70 && score <= 79) {
            grade = 'C';
        } else {
            grade = 'F';
            option = ' ';
        }

        System.out.printf("학점은 %c%c입니다.", grade, option);

        // 4. scanner 닫기(오류 방지)
        input.close();
	
	}
}