본문 바로가기
TIL/JAVA

23.05.02

by J1-H00N 2023. 5. 2.

continue

package nadocoding.Chap_04;

public class _11_Continue {
    public static void main(String[] args) {
        // Continue

        // 치킨 주문 손님중에 노쇼 손님이 있다고 가정
        // For
        int max = 20;
        int sold = 0;
        int noShow = 17;
        for (int i = 1; i <= 50; i++) {
            System.out.println(i + "번 손님, 주문하신 치킨 나왔습니다.");

            // 치킨을 가져가는 상황
            // 손님이 없다면??
            if (i == noShow) {
                System.out.println(i + "번 손님, 노쇼로 인해 다음 손님에게 기회가 넘어갑니다.");
                continue; // 아래 명령은 모두 건너뜀
            }

            sold++;
            if (sold == max) {
                System.out.println("금일 재료가 모두 소진되었습니다.");
                break;
            }
        }
        System.out.println("영업을 종료합니다.");

        System.out.println("----------------");

        // while 문
        sold = 0;
        int index = 1;
        while (index <= 50) {
            System.out.println(index + "번 손님, 주문하신 치킨 나왔습니다.");

            // noshow
            if (index == noShow) {
                System.out.println(index + "번 손님, 노쇼로 인해 다음 손님에게 기회가 넘어갑니다.");
                index++;
                continue; // 아래 명령은 모두 건너뜀
            }
            sold++;
            if (sold == max) {
                System.out.println("금일 재료가 모두 소진되었습니다.");
                break;
            }
            index++;
        }
        System.out.println("영업을 종료합니다.");
    }
}​
package nadocoding.Chap_04;

public class _06_While {
    public static void main(String[] args) {
        // 반복문 while
        int distance = 25;
        int move = 0;
        while (move < distance) {
            System.out.println("발차기를 계속 합니다.");
            System.out.println("현재 이동거리는 " + move);
            move += 3;
        }
        System.out.println("도착하였습니다.");
    }
}

do while

package nadocoding.Chap_04;

public class _07_DoWhile {
    public static void main(String[] args) {
        // 반복문 do while
        int distance = 25;
        int move = 0;
        int height = 2;
        while (move + height < distance) {
            System.out.println("발차기를 계속 합니다.");
            System.out.println("현재 이동 거리 : " + move);
            move += 3;
        }
        System.out.println("도착했습니다.");

        System.out.println("----반복문 #1-----");

        move = 0;
        height = 25;
        while (move + height < distance) {
            System.out.println("발차기를 계속 합니다.");
            System.out.println("현재 이동 거리 : " + move);
            move += 3;
        }
        System.out.println("도착했습니다.");

        System.out.println("----반복문 #2-----");

        // do while 반복문
        do {
            System.out.println("발차기를 계속 합니다.");
            System.out.println("현재 이동 거리 : " + move);
            move += 3;
        } while (move + height < distance);
        System.out.println("도착했습니다.");
    }
}

 

package nadocoding.Chap_04;

public class _08_NestedLoop {
    public static void main(String[] args) {
        // 이중 반복문

        // 별 (*) 사각형 만들기
        /*

        *****
        *****
        *****
        *****
        *****

         */
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // 별 (*) 계단 만들기
        /*

        *
        **
        ***
        ****
        *****
         */
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // 별(*) 역계단 만들기
        /*

             *
            **
           ***
          ****
         *****
         */
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 4-i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < i+1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

 

package nadocoding.Chap_04;

public class _09_MultipleTable {
    public static void main(String[] args) {
        // 구구단

        for (int i = 2; i <= 9 ; i++) {
            for (int j = 1; j <= 9; j++) {
                System.out.println(i + " x " + j + " = " + i*j);
            }
            System.out.println();
        }
    }
}

 

package nadocoding.Chap_04;

public class _10_Break {
    public static void main(String[] args) {
        // break
        // 치킨집에서 매일 20마리만 판매 (1인당 1마리만 구매 가능)
        // 손님이 50명 대기

        // for 문
        int max = 20;
        for (int i = 1; i <= 50; i++) {
            System.out.println(i + "번 손님, 주문하신 치킨 나왔습니다.");
            if ( i == max) {
                System.out.println("금일 재료가 모두 소진되었습니다.");
                break; // 반복문을 탈출
            }
        }
        System.out.println("영업을 종료합니다.");

        System.out.println("------------------");

        // while 문
        int index = 1;
        while (index <= 50) {
            System.out.println(index + "번 손님, 주문하신 치킨 나왔습니다.");
            if (index == max) {
                System.out.println("금일 재료가 모두 소진되었습니다.");
                break;
            }
            index++;
        }
        System.out.println("영업을 종료합니다.");
    }
}

'TIL > JAVA' 카테고리의 다른 글

23.05.04  (0) 2023.05.04
23.05.03  (0) 2023.05.03
23.05.01  (0) 2023.05.01
23.04.28  (0) 2023.04.28
23.04.27  (0) 2023.04.27