본문 바로가기

JAVA

switch/case

switch/case - if문이랑 비슷하지만 좀 더 정형화된 조건문

 

switch/case문 예시

public class zzzz {
    public static void main(String[] args) {
        int num = 3;
        String animal = "";

        switch (num) {
            case 1: animal = "사자";
                break;
                
            case 2: animal = "토끼";
                break;
                
            case 3: animal = "호랑이";
                break;
                
            case 4: animal = "곰";
                break;
                
            default: animal = "동물이 아닙니다";
                break;
        }

        System.out.println(animal);

    }
}
//호랑이  //int num의 값이 3이니깐 3번째인 case3 호랑이가 출력됨
반응형

'JAVA' 카테고리의 다른 글

for (반복문)  (0) 2022.10.19
while (반복문)  (0) 2022.10.13
if (참, 거짓)  (0) 2022.10.13
형 변환, final  (0) 2022.10.12
Set (집합)  (0) 2022.10.11