'switch문'에 해당되는 글 1건

  1. 2012.07.28 switch문

* switch

- switch(식){<- 변수 메소드 호출 type(식에 들어 갈수 있음) :  byte, short, char, int,Enum(jdk 1.5버전부터됨), String(1.7버전부터됨)
case 값1 :
실행구문 A
case 값2 :
실행구문 B
default:
실행구문 C
}
* default는 생략가능
식의 값이 값1이면 A를 실행 , 식의값이 값2이면 B실행 , 값1도 값2도 아니면 default 실행.


(ex :

int i = 2;
switch(i){
case 1:
s.o.p("일");
break;
case 2:
s.o.p("이");
break;
case 3:
s.o.p("삼");
break;
default : 
s.o.p("그밖에");

}


2.
class SwitchTest1{
 public static void main(String[] args)
 {
  //Math.random() : double -> 0<=x<1 실행시 마다 임의적으로 return
  int num = (int)(Math.random()*4)+1; //1~3까지의 수를 찍고 싶으면
  //int num = (int)(Math.random()*51);//0~50을 찍고 싶으면
  System.out.println("num : "+num);
  switch(num){
   case 1:
    System.out.println("스페이드");
   break;
   case 2:
    System.out.println("다이아몬드");
   break;
   case 3:
    System.out.println("클로버");
   break;
   case 4:
    System.out.println("하트");
   break;
  }
 }
}

 

'프로그래밍 > JAVA프로그래밍' 카테고리의 다른 글

배열  (0) 2012.07.28
반복분(for, while)  (0) 2012.07.28
if문  (0) 2012.07.28
Data type, 연산자, 형변환  (0) 2012.07.28
제한자, 캡슐화, 변수, this  (0) 2012.07.28
Posted by 조은성
,