IT/JAVA

[JAVA] 조건문 : switch-case 문

밍띠이 2019. 2. 12. 17:13
반응형

switch - case 문

public static void main(String[] args) {
        int i = 3;

        switch (i) {
        case 1:         // 1 인 경우
            System.out.println("1입니다.");
            break;
        case 2:            // 2 인 경우
            System.out.println("2입니다.");
            break;
        case 3:            // 3 인 경우
            System.out.println("3입니다.");
            break;
        default:        // 모두 해당이 안되는 경우
            System.out.println("기타");
            break;    
        }
    }

case ~ break; 까지 출력
default : if-else 문에서 else와 동일

연습문제 Ex10.java

Switch-Case 예제(Ex10.java)
1~3까지의 숫자를 입력받아 몇인지 보여주는 프로그램을 작성하세요
그외 숫자는 잘못 입력 되었습니다. 라고 출력하세요.
public static void main(String[] args) {
    int userNum = 0;
    Scanner scan = new Scanner(System.in);
    userNum = scan.nextInt();

    switch(userNum) {
    // case 여러개 작성!
        case 1:
        case 2:
        case 3:
            System.out.println(usernum);
            break;
        default:
            System.out.println("잘못 입력 하셨습니다.");
            break;
    }
}

Note Ctrl + Shift + F : 코드 정리 단축키

Note sysout 타이핑 후 Ctrl + Shift + Space Bar : System.out.println(); 단축키

Note Ctrl + Shift + O : import문 단축키

연습문제 Ex11.java

월을 입력받아 해당 월의 일수를 보여주는 프로그램을 작성하시오
- 1, 3, 5, 7, 8, 10, 12월 : 31일까지
- 4, 6, 9, 11 월 : 30일까지
- 2 월 : 28일까지
public static void main(String[] args){
    int month = 0;
    Scanner scan = new Scanner(System.in);
    System.out.print("월을 입력해 주세요 : ");
    month = scan.nextInt();

    switch(month) {
    case 1:    case 3: case 5: case 7:
    case 8: case 10: case 12:
        System.out.println("31일 까지 입니다.");
        break;
    case 4: case 6: case 11:
        System.out.println("30일 까지 입니다.");
        break;
    default:
        System.out.println("28일 까지 입니다.");
        break;
    }
}

Note Switch-case문은 int , char (정수형)에서만 사용 가능

연습문제 Ex12.java

월, 일을 따로 입력받아 별자리를 출력하는 프로그램을 작성하여라.
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int month = 0;
    int day = 0;
    System.out.print("월을 입력해 주세요: ");
    month = scan.nextInt();
    System.out.print("일을 입력해 주세요: ");
    day = scan.nextInt();

// switch문 안에 if문 사용가능
    switch(month) {
        case 1:
            if (day >=20) {
                System.out.println("물병자리");
            } else {
                System.out.println("염소자리");
            }
            break;
        case 2:
            if (day >=19) {
                System.out.println("물고기자리");
            } else {
                System.out.println("물병자리");
            }
            break;
        case 3:
            if (day >=21) {
                System.out.println("양자리");
            } else {
                System.out.println("물고기자리");
            }
            break;
        case 4:
            if (day >=20) {
                System.out.println("황소자리");
            } else {
                System.out.println("양자리");
            }
            break;
        case 5:
            if (day >=21) {
                System.out.println("쌍둥이자리");
            } else {
                System.out.println("황소자리");
            }
            break;
        case 6:
            if (day >=22) {
                System.out.println("게자리");
            } else {
                System.out.println("쌍둥이자리");
            }
            break;
        case 7:
            if (day >=23) {
                System.out.println("사자자리");
            } else {
                System.out.println("게자리");
            }
            break;
        case 8:
            if (day >=23) {
                System.out.println("처녀자리");
            } else {
                System.out.println("사자자리");
            }
            break;
        case 9:
            if (day >=24) {
                System.out.println("천칭자리");
            } else {
                System.out.println("처녀자리");
            }
            break;
        case 10:
            if (day >=23) {
                System.out.println("전갈자리");
            } else {
                System.out.println("천칭자리");
            }
            break;
        case 11:
            if (day >=23) {
                System.out.println("사수자리");
            } else {
                System.out.println("전갈자리");
            }
            break;
        case 12:
            if (day >=25) {
                System.out.println("염소자리");
            } else {
                System.out.println("사수자리");
            }
            break;

        }

    }

Note switch문 안에 if문 사용가능


        System.out.print("생일을 네자리로 입력해주세요(ex>1231): ");
        date = sc.nextInt();

        month = date / 100;    // 월 = 앞 두자리
        day = date % 100;    // 일 = 뒤 두자리

한번에 입력 받아 나누기 (/) , 나머지(%)를 활용해 자를 수 있다.

     System.out.printf("%d월 %d일은 %s자리 입니다.", month, day, result);

System.out.printf();를 활용하여 출력 포멧을 지정해 줄 수있다.

연습문제 (Ex13.java)

점수를 받아 A, B, C, D, F 인지를 출력하는 프로그램을 작성하시오.
public static void main(String[] args) {
        int score = 0;
        String result = "";
        Scanner sc = new Scanner(System.in);
        System.out.print("점수를 입력하세요 : ");
        score = sc.nextInt();

        // switch 문에 변수 조건 설정 가능
        switch(score / 10) {
        case 10:
        case 9:
            result = "A";
            break;
        case 8:
            result = "B";
            break;
        case 7:
            result = "C";
            break;
        case 6:
            result = "D";
            break;
        default:
            result = "F";
            break;
        } 
        System.out.printf("당신의 학점은 %s 입니다.", result);
    }

Note switch 문에 변수 조건 설정 가능

String 값 비교하기(Str_equals.java)

public static void main(String[] args) {
        String str = "abc";
        String str2 = "abc";
        System.out.println(str == str2);        //true
        System.out.println(str.equals("abc"));     // true
        System.out.println(str.equals(str2));        //true

        String str3 = "ABC";
        System.out.println(str3.toLowerCase());             //abc 소문자로 변환
        System.out.println(str.equals(str3));                 //false
        System.out.println(str.equals(str3.toLowerCase())); // true
    }

Note 객체 비교시 (==) 사용 보다는 equals를 사용해 주어야 한다.(값비교)

equals는 대소문자 구별 O

필요시 .toLowerCase()를 사용하여 대소문자 통일하여 비교

모바일] git 으로 보기

반응형