JAVA

명품 자바 에센셜 2강 실습문제 7번

IT 정복가 2022. 3. 7. 19:18
728x90

학점이 A, B이면 "Excellent", 학점이 C, D이면 "Good", 학점이 F이면 "Bye"라고 출력하는 프로그램을 작성하라. switch와 break을 활용하라.

학점을 입력하세요>>B
Excellent

답:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		System.out.print("학점을 입력하세요>>");
		String grade = sc.next();
		
		switch(grade) {
			case "A":
			case "B":
				System.out.println("Excellent"); break;
			case "C":
			case "D":
				System.out.println("Good"); break;
			case "F":
				System.out.println("Bye"); break;
		}
			
		sc.close();
	}

}

case들마다 같은 결과를 출력하고 싶다면 case를 여러개 나열할 수 있습니다.

728x90