스크린샷 2025-08-11 오후 4.31.20.png

package com.haenin.section02.looping;

import java.util.Scanner;

public class Application1 {
    public static void main(String[] args) {
        A_for a = new A_for();
//        a.testSimpleForStatement();
//        a.testForExample();
//        a.testForExample2();

        B_nestedFor b = new B_nestedFor();
//        b.printGugudanFromTwoToNine();

        C_While c = new C_While();
        c.testWhileExample();

        D_doWhile d = new D_doWhile();
        d.testSimplieDoWhileStatement();
        d.testDoWhileExample();

    }
}

package com.haenin.section02.looping;

import java.util.Scanner;

public class A_for {
    public void testSimpleForStatement() {
        /* 설명. 1부터 10까지 출력하는 반복문 작성하기 */
        for(int i=0; i<=10;i++){
            System.out.println("i = " + i);
        }
    }

    public void testForExample() {
        /* 설명. 5개의 각 변수에 값들에 2를 곱하고 1을 더해 sum에 누적 */
        int num1 = 1;
        int num2 = 2;
        int num3 = 3;
        int num4 = 4;
        int num5 = 5;

        int sum = 0;

        sum += num1 * 2 + 1;
        sum += num2 * 2 + 1;
        sum += num3 * 2 + 1;
        sum += num4 * 2 + 1;
        sum += num5 * 2 + 1;

        System.out.println("sum = " + sum);

        /* 설명. for문을 통해 개선해보자(규칙적이며 반복적인 코드들) */
        int forSUm = 0;

        /* 설명. 디버깅요령: 1. 반드시 되는 시점까지 돌리거나 주석해서 되게 만든다.
        *                2. 코드를 한번에 많이 완벽하게 하고 실행하면 X
        * */
        for (int i = 0; i < 5 ; i++) {
            System.out.println("i = " + i);
//            forSUm += (i + 1) * 2 + 1;

        }
        }

    public void testForExample2() {
        Scanner sc = new Scanner(System.in);
        System.out.println("반복횟수 입력: ");
        int limit = sc.nextInt();

        int sum = 0;
        for (int i = 0; i < limit; i++) {
            sum += (i+1);

        }
        System.out.println("sum = " + sum);
    }
}

package com.haenin.section02.looping;

import java.util.Scanner;

public class B_nestedFor {
    public void printGugudanFromTwoToNine() {
        /* 설명.
         *   2단
         *   2 * 1 = 2
         *   2 * 2 = 4
         *   ...
         *   9단
         *   9 * 1 = 9
         *   9 * 2 = 18
         *   ...
         *   9 * 9 = 81
         *   컬럼을 다 흛고 다음 행으로 */

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

        for (int i = 2; i <= 9; i++) {
            System.out.println(i + " 딘");
            printGugudan(i);
            System.out.println("=============");

        }

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

    }
    /* 설명. 단수가 넘어오면 해당 단수의 구구단 출력을 담당하는 메소드  */
    private static void printGugudan ( int i){
        for (int j = 1; j <= 9; j++) {
            System.out.println("i * j = " + i * j);
        }
    }
    public void testForExample(){
        /* 설명. 별찍기
         *   양의 정수 하나를 입력하세요
         *       *
         *      **
         *     ***
         *    ****
         *   *****
         * */
        Scanner sc = new Scanner(System.in);
        System.out.println("양의 정수 하나를 입력하세요: ");
        int input = sc.nextInt();

        for (int i = 0; i < input; i++) {
            /* 설명. 공백찍기 */
            for (int j = 0; j < input - (i + 1) ; j++) {
                System.out.print(" ");
            }

            /* 설명. 별찍기 */
            for (int j = 0; j < i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

package com.haenin.section02.looping;

import java.util.Scanner;

public class C_While {
    /* 설명. 입력한 사용자의 값에 따라 반복횟수가 정해 지는 형태에서 사용 */
    public void testWhileExample() {
        Scanner sc = new Scanner(System.in);

        /* 설명. 'y' 또는 'Y'를 입력하면 반복문을 멈추는 while문 */
        char answer = '\\0'; /* 필기. '\\u0000'과 동일 */
        while(!(answer =='y'|| answer=='Y')){
            System.out.println("탈출하고 싶다면 y 또는 Y를 입력하세요.");
            answer = sc.next().charAt(0);
        }
        System.out.println("천국에 오신 것을 환영합니다. ");
    }
}

package com.haenin.section02.looping;

import java.util.Scanner;

public class D_doWhile {
    public void testSimplieDoWhileStatement() {
        do{
            System.out.println("반복문 실행될까");
        } while (false);
        System.out.println("반복 종료 이후");
    }
    public void testDoWhileExample() {

        int sumPrice = 0;
        Scanner sc = new Scanner(System.in);
        int choice = 0;
        do {
            System.out.println("sumPrice에 담을래 뺼래?");
            System.out.println("1. 담을래(+1000)");
            System.out.println("2. 뺄래(-1000)");
            System.out.println("3. 나갈래");
            System.out.println("번호를 고르세요: ");
            choice = sc.nextInt();
            if (choice == 1){
                sumPrice += 1000;
            }else if(choice == 2){
                sumPrice -= 1000;
            }
        }while (choice != 3);
        System.out.println("최종 합계는 " + sumPrice);
    }

}