(7) While & Do ~ While & Continue

JAVA/JAVA 이론 2018. 10. 24. 19:29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package Studypackage;
 
public class studyclass 
{
    // while 반복문
    // 참고 : for(i=0;i<10;i++) {....}
    // 증감식 -> i++ , i-- , i+3 , i-=3 ....
    public static void main(String[] args)
    {
        int i;
        i = 0;
        while(i<10)    // 반복 실행할 조건식
        {
            System.out.println("i = " + i);
            System.out.println("Hello Java~!!");
            i++;

        }
    }
}
 
cs



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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Studypackage;
 
import java.util.Scanner;
 
public class studyclass 
{
    // 특정한 선택에 대해 조건이 참 또는 거짓이 되는 경우
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int sel;
        
        System.out.println("선택 하시오");
        System.out.println("1] 장비  2] 소비  3] 병력  4] 종료");
        
        while(true)        // 무한반복
        {
            System.out.print("선택 >>> ");
            sel = sc.nextInt();
            String temp = "";
            /*
             if(sel == 1)
             {
                 temp ="장비";
             }
             else if(sel == 2)
             {
                 temp = "소비";
             }
             else if(sel == 3)
             {
                 temp = "병력";
             }
             else if(sel == 4)    // while 종료
             {
                 break;
             }
             else
             {
                 temp ="잘못된 값";
             } 
             */
            if(sel == 4break;        // while 종료 조건
            switch(sel)
            {
            case 1: temp ="장비";break;
            case 2: temp ="소비";break;
            case 3: temp ="병력";break;
            //case 4: break;    // switch 종료
            default: temp ="잘못된 값";
            }
            
            System.out.println(temp + "을(를) 생성 합니다");
        } // while end
        
        System.out.println("END!!");


    }
}
 
cs



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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package Studypackage;
 
import java.util.Scanner;
 
public class studyclass 
{
    // 특정한 선택에 대해 조건이 참 또는 거짓이 되는 경우
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int sel = 0;
        
        System.out.println("아이템을 선택하세요");
        System.out.println("1]소비  2]장비  3]병력  4]종료");
        
        while(sel != 4)        // 반복 조건
        {
            System.out.print("선택 >>> ");
            sel = sc.nextInt();
            String temp ="";
            
            switch(sel)
            {
            case 1: temp ="소비";break;
            case 2: temp ="장비";break;
            case 3: temp ="병력";break;
            //case 4: break;    // switch 종료
            default: temp ="잘못된 값";
            }
            
            if(sel == 4)
            {
                continue;
            }
            else
            {
                System.out.println(temp + "을(를) 생성 합니다");
            }
        }    // while end
        
        System.out.println("END!!");


    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package Studypackage;
 
public class studyclass 
{
    // while 과 do while
    public static void main(String[] args)
    {
        int i = 10;
        while(i<10)
        {
            System.out.println("Hello Java !!");
            i++;
        }    // 실행 횟수 - 0 번
        do    // 최소 1번 반복하는 명령문
        {
            System.out.println("GoodBye Java !!");
            i++;
        }while(i<10);    // 실행 횟수 - 1번


    }
}
 
 
cs



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
29
30
31
32
33
34
package Studypackage;
 
public class studyclass 
{
    // 반복문에서 break , continue 테스트
    public static void main(String[] args)
    {
        System.out.println("break 테스트");
        for(int i=0; i<10; i++)
        {
            // 변수 i는 for문 안에서만 사용하는 변수
            if(i==5)
            {
                break;    // for 반복문 종료
            }
            System.out.println("i = " + i);
            System.out.println("Hello Java!!");


        }
        System.out.println();
        System.out.println("continue 테스트");
        for(int i=0;i<10;i++)
        {
            // 변수 i for문 안에서만 사용하는 변수
            // if(i==5)
            if(i%3 == 0)
            {
                continue// 이하 명령들 건너뛰고 i증감후 반복 처음부터 
            }
            System.out.println("i = " + i);
            System.out.println("Hello Java");

        }
    }
}
 
cs


'JAVA > JAVA 이론' 카테고리의 다른 글

(9) String Method  (0) 2018.10.28
(8) Array  (0) 2018.10.25
(6) If & For  (0) 2018.10.23
(5) Op & IF & Char  (0) 2018.10.22
(4) String & Boolean  (0) 2018.10.21

설정

트랙백

댓글