자체임상실험
Inform Restaurant
자체임상실험
전체 방문자
오늘
어제
  • 분류 전체보기 (89)
    • IT정보와 지식 TABLE (1)
      • IT정보 및 꿀팁 (0)
      • Windows (1)
      • Linux (0)
    • Linux TABLE (0)
      • 취약점 점검 스크립트 (0)
    • Developer TABLE (33)
      • Java (33)
      • Java Algorithm (0)
    • DataBase TABLE (1)
      • SQLD-P (1)
    • Cloud TALBE (0)
      • AWS (0)
    • Security TABLE (1)
      • 비박스(bee-box) 취약점 점검 (1)
      • 보안기사 (0)
    • Writer TABLE (42)
      • 기사필사 (42)
      • 서평 (0)
    • Growth TABLE (10)
      • 국민취업지원제도 (4)
      • 국비학원 (6)
    • Life TABLE (0)
      • 자서전 (0)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 동아일보
  • Switch
  • for
  • 경향신문
  • 논설위원
  • 조건문
  • 디엠제트
  • do while
  • 반복문
  • 국민취업지원제도
  • KH정보교육원
  • 초장기 주택담보대출
  • 조용한 고용
  • while
  • @
  • 국비학원
  • 2차원배열
  • 천자칼럼
  • Java
  • 배열복사
  • 방사능
  • 지하공간
  • 해양오염수
  • 문자열비교
  • if
  • 논설위원필사
  • 배열
  • Arrays
  • array
  • 한겨레

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
자체임상실험
Developer TABLE/Java

조건문 switch

Developer TABLE/Java

조건문 switch

2021. 8. 25. 20:53

switch 문

  • 조건식 하나로 처리해야 하는 경우의 수가 많을 때 사용
  • 조건식의 결과는 정수 또는 문자열
  • case문의 값은 정수, 상수(문자 포함), 문자열만 가능
  • default는 모든 조건식이 불일치 시 실행하며 생략가능
  • break문이나 switch문의 끝을 만나면 조건문 종료

예제

switch (조건식) {
    case 값1 :
	// 조건식의 결과가 값1과 만족할 경우 수행
    break;    // switch문을 벗어난다.
    
    ...
    
    default :
	// 조건식이 모두 만족하지 않을 때 수행

}

과일 가격 출력

public void method1() {
	
	String sFruit ="";
	int nPrice = 0;
	
	Scanner s = new Scanner(System.in);
	
	System.out.printf("Tell me what fruit you want to eat: ");
	sFruit = s.nextLine();
	
	switch(sFruit) {
	case "peach" : 
		nPrice = 800;
		
		break;
	case "watermelon" :
		nPrice = 3800;
		
		break;
	case "orange" :
		nPrice = 450;
		
		break;
	case "apple" :
		nPrice = 300;
		
		break;
	default	:
		System.out.println("SOLD OUT!!");
	}
	
	System.out.printf("%s costs ₩%d\n", sFruit, nPrice);
}

출력값

Tell me what fruit you want to eat: apple
apple costs ₩300

Tell me what fruit you want to eat: banana
SOLD OUT!!

월의 마지막 일수 출력

public void method2() {
	int nMonth = 0;
	
	Scanner s = new Scanner(System.in);
	
	System.out.printf("Insert Month(1~12): ");
	nMonth = s.nextInt();
	
	if(nMonth < 1 || nMonth > 12) {
		System.out.println("Are your eyes twisted?, God damn");	
		return;
	}
    
	switch(nMonth) {
	case 1 :
	case 3 :
	case 5 :
	case 7 :
	case 8 :
	case 10 :
	case 12 :
		System.out.printf("The Month you insert is 31st\n");
		break;
	case 4 :
	case 6 :
	case 9 :
	case 11 :
		System.out.printf("The Month you insert is 30st\n");
		break;
	case 2:
		System.out.printf("The Month you insert is 28 or 29st\n");
		break;		
	}	
}

출력값

Insert Month(1~12): 1
The Month you insert is 31st

Insert Month(1~12): 9
The Month you insert is 30st

Insert Month(1~12): 2
The Month you insert is 28 or 29st

Insert Month(1~12): 13
Are your eyes twisted?, God damn
저작자표시 비영리 (새창열림)

'Developer TABLE > Java' 카테고리의 다른 글

반복문(제어문)  (0) 2021.08.26
조건문 if (if ~ else , if ~ else if ~ else)  (0) 2021.08.25
조건문(제어문)  (0) 2021.08.25
  • switch 문
  • 예제
'Developer TABLE/Java' 카테고리의 다른 글
  • 반복문 for
  • 반복문(제어문)
  • 조건문 if (if ~ else , if ~ else if ~ else)
  • 조건문(제어문)
자체임상실험
자체임상실험
생활에 유용한 정보와 일상을 담은 휴식처, Sobremesa 입니다.

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.