ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210804_조건문Switch
    JAVA 2021. 8. 4. 11:04

    swith문

    If문의 조건값은 boolean형인데에 비해 switch문의 조건값은 long형을 제외한 정수형(byte,short,int) 또는 char형인 것이 다르다.

    입력할 때 case뒤에 오는 조건값이 중복되지 않도록 한다. 그렇지 않으면 case를 구분하는 값이 복제되어 중복되었다는 오류 발생.

     

    Scanner sc=new Scanner(System.in);

     

    int num;

     

    System.out.println("숫자입력");

    num=sc.nextInt();

     

    switch (num) {

    case 1:  //콜론 사용

    System.out.println("1이 출력됨");

    break;  //break 안 하면 계속 머물러짐, 빠져나가야함

    case 2:

    System.out.println("2가 출력됨");

    break;

    case 3:

    System.out.println("3이 출력됨");

    break;

     

    default:  //else에 해당됨 default는 뭐 없이 바로 콜론

    System.out.println("not 1,2,3");

    }

     

    //결과

    숫자입력

    6

    not 1,2,3

     

    -------------------

    'JAVA' 카테고리의 다른 글

    210804_for,while,break,continue(정의)  (0) 2021.08.04
    210804_charAt,substring,equal  (0) 2021.08.04
    210803_조건문if  (0) 2021.08.04
    210803_연산자  (0) 2021.08.03
    210803_스캐너  (0) 2021.08.03

    댓글

Designed by Tistory.