[백준 브론즈 V] 9498번: 시험 성적

DONI·2021년 8월 6일
0

Baekjoon Online Judge

목록 보기
14/31
post-thumbnail

문제

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


입력

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


출력

시험 성적을 출력한다.


예제 입력 1

100

예제 출력 1

A


소스코드

  • Java 첫 번째 방법 : if 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int score = sc.nextInt();
		sc.close();
		if (score >= 90) System.out.print("A");
		else if (score >= 80) System.out.print("B");
		else if (score >= 70) System.out.print("C");
		else if (score >= 60) System.out.print("D");
		else System.out.print("F");
	}
}
  • Java 두 번째 방법 : switch 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int score = sc.nextInt();
		sc.close();		
		switch (score / 10) {
		case 10:
			System.out.print("A");
			break;
		case 9:
			System.out.print("A");
			break;
		case 8:
			System.out.print("B");
			break;
		case 7:
			System.out.print("C");
			break;
		case 6:
			System.out.print("D");
			break;
		default:
			System.out.print("F");
			break;
		}
	}
}
  • Java 세 번째 방법 : 삼항연산자
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int score = sc.nextInt();
		sc.close();		
		System.out.print(score >= 90 ? "A" : score >= 80 ? 
        "B" : score >= 70 ? "C" : score >= 60 ? "D" : "F");
	}
}

[바로가기] 9498번: 시험 성적

profile
틀린 내용이 있다면 댓글 또는 이메일로 알려주세요 ❤ꔛ❜

0개의 댓글