BAEK_9498(시험 성적)

Max·2022년 8월 20일
0

BAEKJOON

목록 보기
2/21

백준 9498 성적 시험



- 문제

시험 점수를 입력받아 90 ~ 100점은 A, 80 ~ 89점은 B, 70 ~ 79점은 C, 60 ~ 69점은 D, 나머지 점수는 F를 출력하는 프로그램을 작성하시오.


- 입력

첫째 줄에 시럼 점수가 주어진다. 시험 점수는 0보다 크거나 같고, 100보다 작거나 같은 정수이다.




- 풀이


  • 방법 1
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
        
        int Score = sc.nextInt();
        
        if(90 <= score && score <= 100) {
        	System.out.println("A");
        } else if (80 <= score && score <= 89) {
        	System.out.println("B");
        } else if (70 <= score && score <= 79) {
        	System.out.println("C");
        } else if (60 <= score && score <= 69) {
        	Syste,.out.println("D");
        } else {
        	System.out.println("F");
        }
     }
  }

Scanner으로 입력받고
if조건문을 통해 입력 값을 비교하고 해당 값을 출력할 수 있는 방법이다.


  • 방법 2
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
	public static void main(String[] args) Throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int score = Integer.parseInt(br.readLine());
        
        if(90 <= score && score <= 100) {
        	System.out.println("A");
        } else if (80 <= score && score <= 89) {
        	System.out.println("B");
        } else if (70 <= score && score <= 79) {
        	System.out.println("C");
        } else if (60 <= score && score <= 69) {
        	Syste,.out.println("D");
        } else {
        	System.out.println("F");
        }
     }
  }

이와 같이 BufferedReader로 입력받아 연산하는 방법이다.


+ 삼항연산자


  • 풀이
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
        
        int Score = sc.nextInt();
        
        System.out.println((A >= 90) ? "A" : (A >= 80) ? "B" : 
        (A >= 70) ? "C" : (A >= 60) ? "D" : "F");
      }
   }

이와 같이 삼항연산자를 이용해 if조건문으로 길었던 연산을 이렇게 줄일 수도 있다.






ref : https://st-lab.tistory.com/22

profile
co_der

0개의 댓글