프로그래밍/Java

자바 Switch 신 기능(자바 12버전 이상)

유니렌 2024. 12. 18. 15:28
package ch04.sec03;

public class SwitchValueExample {

	public static void main(String[] args) {
		String grade="B";
		
		//11이전
		int score1=0;
		
		switch (grade) {
		case "A":
			score1=100;
			break;
		case "B":
			int result=100-20;
			score1=result;
			break;
		default:
			score1=60;
			break;
		}
		System.out.println("score1: "+score1);
		
		//13버전이상
		int score2=switch(grade) {
		case"A"->100;
		case"B"->{
			int result=100-20;
			yield result;
		}
		default->60;
		};
		System.out.println("score2: "+score2);
	}

}​

 

13이상

package ch04.sec03;

public class SwitchValueExample {

	public static void main(String[] args) {
		String grade="B";
		
		//11이전
		int score1=0;
		
		switch (grade) {
		case "A":
			score1=100;
			break;
		case "B":
			int result=100-20;
			score1=result;
			break;
		default:
			score1=60;
			break;
		}
		System.out.println("score1: "+score1);
		
		//13버전이상
		int score2=switch(grade) {
		case"A"->100;
		case"B"->{
			int result=100-20;
			yield result;
		}
		default->60;
		};
		System.out.println("score2: "+score2);
	}

}

 

 

 

'프로그래밍 > Java' 카테고리의 다른 글

메모리 확인 하는 법  (0) 2024.12.19
If문 실습_3  (0) 2024.12.19
Switch 실습_1  (0) 2024.12.18
Switch문  (0) 2024.12.18
If문 실습_2 버스 터널  (0) 2024.12.18