티스토리 뷰

Programing/Java

Java1 - 12. while문

HNNN 2017. 4. 26. 15:49


 while(논리값){ 


true 무조건 반복
  -->   false가 될 때까지


}


#실습 1.


package loopex;

public class WhileTest {

 public static void main(String[] args) {


  for(int i =0; i<10; i++){
       System.out.println("자바야");
   
       int num=0;
       while(num<10){
            num++;
            System.out.println(num+"안녕");
       }//while 문 end
  }//for문 end
  
 }

}





do{
 
 }while(논리값);
 
 if(조건식)
  문장;
 
 for(;;)
  문장;
  
 while(논)
  문장;
  


 do{
 
 }while(논리값); 

--> " ; "  은 종결

     do~while로 끝나는 것이다 라는 뜻
 

 ※ 처음부터 조건이 거짓이어도 한번은 반드시 실행한다.
 ※ 반드시 한번은 시작하고 싶을 때 do~while을 선택하자
 
  ※ for : 반복 횟수가 정해져 있을 때 사용 
  ※ while : 반복 횟수없이 무작정 돌리고 싶을 때 사용



#실습



=>1234의 비밀번호를 칠 기회를 3번을 주자




package loopex;

import javax.swing.JOptionPane;

public class DoWhileTest {
 public static void main(String[] args) {

  
  String pw="";
  int cnt=0;
  String msg="비밀번호를 입력하세요";
  
  do{
   pw= JOptionPane.showInputDialog(msg);
   cnt++;
   if(cnt==3){
    break;
   }
   
  }while(!pw.equals("1234") );
  if(cnt==3){
   System.out.println("가까운은행에 가세요");
  
  }else{
   String menu="비번을 통과했군요 \n1.잔액조회 \n2. 계좌이체";
   JOptionPane.showMessageDialog(null, menu);
   
  }
  
 }
}





 continue : 반복문 수행 중, 특정 위치로 바로 이동하는 제어문
 
  for: 증감식으로 이동할 것
  while,do~while : 조건식으로 이동할 것
  

 ( if, switch,  for,  doWhile, while,  continue, return,  break )


#실습

  1) 1~100까지, 3의 배수를 출력 
  2) 그중에서 9의 배수는 출력하지 말 것
    
    //1~100 까지 출력
    //if문 제어(3의 배수를 출력)


for(int i=1; i<101; i++){

if(i%3==0 && i%9!=0){
    System.out.println(i);

}

}

<continue 사용 >



package loopex;

public class ContinueTest {

 public static void main(String[] args) {
  
     
      int i=0;
      for (i=1; i<101;i++){
           f(i%9==0 )
           continue; //i의 증감식으로 감
   
           if(i%3==0){
           System.out.println(i);
           }
   
      }
  

 }

}

'Programing > Java' 카테고리의 다른 글

Java1 - 14. class  (0) 2017.04.26
Java 이용 - ATM기 작성  (0) 2017.04.26
Java1 - 11. For 문  (0) 2017.04.26
Java1 - 10. Switch  (0) 2017.04.21
Java1 - 09. ifelse 제어문  (0) 2017.04.20
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함