<문제>

switch 구문을 하용하여 세 과목을 입력 받은 후,

평균 90 이상 A, 80 이상 B, 70 이상 C, 나머지 F

(단, 한 과목이라도 50점 이하시, 무조건 F)

 

 

public class test {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		
		System.out.print("수학 성적 : ");
		int math = sc.nextInt();
		
		System.out.print("과학 성적 : ");
		int sci = sc.nextInt();
		
		System.out.print("영어 성적 : ");
		int eng = sc.nextInt();
		
		int tot = math + sci + eng;
		int avg = tot/3;

		char grade = 'F';
		
		if(math < 50 || sci < 50 || eng < 50) {	// 한 과목이라도 50점 이하시 F

		}else {
			
			switch((int)avg/10) {
			case 10:
			case 9 : {grade = 'A'; break;}  // 90~99 까지
			case 8 : {grade = 'B'; break;}
			case 7 : {grade = 'C'; break;}	
			default : {grade = 'F';}
			}
			
		}
		
		System.out.println("평균 : " + avg);
		System.out.println("학점 : " + grade);

	}

}

 

<실행 결과>

+ Recent posts