switch

package sec04.chap02;

public class Ex01 {
    public static void main(String[] args) {
        int fingersOut = 2;

        //  ๐Ÿ’ก switch : ๊ด„ํ˜ธ ์•ˆ์— ๊ธฐ์ค€์ด ๋  ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์Œ
        //  ๊ฐ€๋Šฅํ•œ ์ž๋ฃŒํ˜•: byte, short, int, char, String, enum(์ดํ›„ ๋ฐฐ์›€)

        switch (fingersOut) {

            //  ๐Ÿ’ก case : ๊ธฐ์ค€์— ์ผ์น˜ํ•˜๋Š” case๋กœ ๋ฐ”๋กœ ์ด๋™
            //  ๐Ÿ’ก break : switch๋ฌธ ์‹คํ–‰์„ ์ข…๋ฃŒ
            //  ๐Ÿ’ก default: ํ•ด๋‹นํ•˜๋Š” case๊ฐ€ ์—†์„ ๋•Œ - ๋งˆ์ง€๋ง‰์— ์ž‘์„ฑ

            case 2:
                System.out.println("๊ฐ€์œ„");
                break;
            case 0:
                System.out.println("๋ฐ”์œ„");
                break;
            case 5:
                System.out.println("๋ณด");
                break;
            default:
                System.out.println("๋ฌดํšจ");
        }
    }
}

package sec04.chap02;

public class Ex02 {
    public static void main(String[] args) {
        String direction = "north";
        String dirKor;

        //  break ์ œ๊ฑฐํ•˜๊ณ  ์‹คํ–‰ํ•ด ๋ณผ ๊ฒƒ
        switch (direction) {
            case "north":
                dirKor = "๋ถ";
                break;
            case "south":
                dirKor = "๋‚จ";
                break;
            case "east":
                dirKor = "๋™";
                break;
            case "west":
                dirKor = "์„œ";
                break;
            default:
                dirKor = null;
        }

        System.out.println(
                dirKor != null ? dirKor : "๋ฌดํšจ"
        );
    }
}

package sec04.chap02;

public class Ex03 {
    public static void main(String[] args) {
        //  ๐Ÿ’ก break ๊ด€๋ จ ๋™์ž‘๋ฐฉ์‹์„ ์ด์šฉ
        char yutnori = '๋„';

        switch (yutnori) {
            case '๋ชจ': System.out.println("์•ž์œผ๋กœ");
            case '์œท': System.out.println("์•ž์œผ๋กœ");
            case '๊ฑธ': System.out.println("์•ž์œผ๋กœ");
            case '๊ฐœ': System.out.println("์•ž์œผ๋กœ");
            case '๋„': System.out.println("์•ž์œผ๋กœ"); break;
            default:
                System.out.println("๋ฌดํšจ");
        }
    }
}

package sec04.chap02;

public class Ex04 {
    public static void main(String[] args) {
        byte month = 1;
        byte season;

        switch (month) {
            case 1: case 2: case 3:
                season = 1;
                break;
            case 4: case 5: case 6:
                season = 2;
                break;
            case 7: case 8: case 9:
                season = 3;
                break;
            case 10: case 11: case 12:
                season = 4;
                break;
            default:
                season = 0;
        }

        System.out.println(
                season > 0
                        ? "์ง€๊ธˆ์€ %d๋ถ„๊ธฐ์ž…๋‹ˆ๋‹ค.".formatted(season)
                        : "๋ฌดํšจํ•œ ์›”์ž…๋‹ˆ๋‹ค."
        );
    }
}

package sec04.chap02;

public class Ex05 {
    public static void main(String[] args) {
        byte startMonth = 1;
        String holidays = "";

        switch (startMonth) {
            case 1:
                holidays += "์„ค๋‚ , ";
            case 2:
            case 3:
                holidays += "3ยท1์ ˆ, ";
                break;
            case 4:
            case 5:
                holidays += "์–ด๋ฆฐ์ด๋‚ , ";
            case 6:
                holidays += "ํ˜„์ถฉ์ผ, ";
                break;
            case 7:
            case 8:
                holidays += "๊ด‘๋ณต์ ˆ, ";
            case 9:
                holidays += "์ถ”์„, ";
                break;
            case 10:
                holidays += "ํ•œ๊ธ€๋‚ , ";
            case 11:
            case 12:
                holidays += "ํฌ๋ฆฌ์Šค๋งˆ์Šค, ";
                break;
            default:
                holidays = null; // ํœด์ผ์ด ์—†๋Š” ๋ถ„๊ธฐ์™€ ๊ตฌ๋ถ„
        }

        String result = holidays == null
                ? "(์ž˜๋ชป๋œ ์›”์ž…๋‹ˆ๋‹ค)"
                : "๋ถ„๊ธฐ ๋‚ด ํœด์ผ: " + holidays
                .substring(0, holidays.lastIndexOf(", "));
    }
}

Last updated