풀스택 개발자 과정 39일차

너구·2026년 7월 1일

풀스택 성장과정

목록 보기
42/79

자료형

package Day02;

public class Casting {
    static void main() {
        System.out.println(7+2.5);
        System.out.println((int)3.5);
        char ch = 'A'; // 65
        System.out.println((char)(ch + 1)); //

        String str = "hello";
        System.out.println(str.length());

        System.out.println("hi"+5+3);
        System.out.println(5+3+"");
        // int k = 5+3+""; // 자료형 안맞아
        String str1 = 5+3+""; // 자료형 맞아

        int x = 10;
        System.out.println(x++); // 10 전치 바로 증가
        System.out.println(++x); // 12 후치 증가가 일어나지 않는다. 그 다음 라인 증가가 일어난다.

        System.out.println(5>3 && 4==7 || 3!=7);

        int a1 = 8;
        int b1 = a1 == 10 ? 1 : 0; // 0
        int b2 = a1 == 10 ? x < 10 ? 5 : 7 : 9; // 9

    }
}

Scanner Test

package Day02;

import java.util.Scanner;

public class ScannerTest {
    static void main() {
        Scanner sc = new Scanner(System.in);

        System.out.print("입력 : ");
        String str1 = sc.next();
        String str2 = sc.next();

        System.out.println(str1 + ", " + str2);
        sc.nextLine();

        System.out.print("입력 : ");
        String str3 = sc.nextLine();

        System.out.println(str3);
    }
}

switch문

package Day02;

public class Condition {
    static void main() {

        int a = 10;
        switch (a) {
            case 1:
            case 2:
            case 3:
                System.out.println("1,2,3중 하나입니다");
                break;
            case 10:
            case 11:
            case 12:
                System.out.println("10,11,12중 하나입니다");
                break;

            default:
                System.out.println("잘못된 입력입니다");
                break;
        }
    }
}

switch문 활용

package Day02;

import java.util.Scanner;

public class Q1 {
    /*
    3월~5월 봄 3~15도
    6월!8월 여름 18~37도
    9월~11월 가을 3~15도
    12월~2월 겨울 2~-15도
    1. 월 1~12가 아니거나 온도 -15~37 아니면 잘못된 입력이다.
    2. 범위 안에 월과 온도가 있는데 맞지 않으면 계절과 온도가 맞지 않다.
    3. 월과 온도가 맞으면 각 계절과 온도를 출력
     */
    static void main() {

        Scanner sc = new Scanner(System.in);

        System.out.print("월 입력: ");
        int user1 = sc.nextInt();

        System.out.print("온도 입력: ");
        int user2 = sc.nextInt();

        switch (user1) {
            case 3, 4, 5:
                if (user2 >= 3 && user2 <= 15) {
                    System.out.print("계절은 봄입니다.");
                    System.out.println(" 온도는 " + user2 + "도입니다.");
                } else {
                    System.out.println("계절과 온도가 맞지 않습니다.");
                }
                break;
            case 6, 7, 8:
                if (user2 >= 18 && user2 <= 37) {
                    System.out.print("계절은 여름입니다.");
                    System.out.println(" 온도는 " + +user2 + "도입니다.");
                } else {
                    System.out.println("계절과 온도가 맞지 않습니다.");
                }
                break;
            case 9, 10, 11:
                if (user2 >= 3 && user2 <= 15) {
                    System.out.print("가을입니다.");
                    System.out.println(" 온도는 " + user2 + "도입니다.");
                } else {
                    System.out.println("계절과 온도가 맞지 않습니다.");
                }
                break;
            case 12, 1, 2:
                if (user2 >= -15 && user2 <= 2) {
                    System.out.print("겨울입니다.");
                    System.out.println(" 온도는 " + user2 + "도입니다.");
                } else {
                    System.out.println("계절과 온도가 맞지 않습니다.");
                }
                break;
        }
        sc.close();
    }
}

switch문 활용 선생님 답

package Day02;

import java.util.Scanner;

public class Q1_1 {
    static void main() {
        /*
        3월~5월 봄 3~15도
        6월!8월 여름 18~37도
        9월~11월 가을 3~15도
        12월~2월 겨울 2~-15도
        1. 월 1~12가 아니거나 온도 -15~37 아니면 잘못된 입력이다.
        2. 범위 안에 월과 온도가 있는데 맞지 않으면 계절과 온도가 맞지 않다.
        3. 월과 온도가 맞으면 각 계절과 온도를 출력
        */

        // 1. 입력
        Scanner sc = new Scanner(System.in);
        System.out.print("월 입력: ");
        int month = sc.nextInt();
        System.out.print("온도 입력: ");
        int temp = sc.nextInt();

        // 2. 조건에 맞는 분기 및 출력
        if (month <1 || month > 12 || temp < -15 || temp > 37) {
            System.out.println("잘못된 입력입니다.");
        } else {
            switch (month) {
                case 3:
                case 4:
                case 5:
                    if (temp > 2 && temp < 16) {
                        System.out.println("계절은 봄입니다. 온도는 " + temp + "입니다.");
                    } else {
                        System.out.println("계절과 온도가 맞지 않습니다.");
                    }
                    break;
                case 6:
                case 7:
                case 8:
                    if (temp > 17 && temp < 38) {
                        System.out.println("계절은 여름입니다. 온도는 " + temp + "입니다.");
                    } else {
                        System.out.println("계절과 온도가 맞지 않습니다.");
                    }
                    break;
                case 9:
                case 10:
                case 11:
                    if (temp > 2 && temp < 16) {
                        System.out.println("계절은 가을입니다. 온도는 " + temp + "입니다.");
                    } else {
                        System.out.println("계절과 온도가 맞지 않습니다.");
                    }
                    break;
                case 12:
                case 1:
                case 2:
                    if (temp > -16 && temp < 3) {
                        System.out.println("계절은 겨울입니다. 온도는 " + temp + "입니다.");
                    } else {
                        System.out.println("계절과 온도가 맞지 않습니다.");
                    }
                    break;
                default:
                    System.out.println("잘못된 입력입니다.");
                    break;

            }
        }
    }
}

while문

package Day02;

public class WhileTest {
    static void main() {

        int hap = 0, count = 1;
        while (count <= 10) {
            hap = hap + count;
            count = count + 1;
        }
        System.out.println("1부터 10까지의 합은 " + hap + "입니다.");

    }
}

for문

package Day02;

public class ForTest {
    static void main() {

        int dan;
        int times;
        for (dan = 2; dan <= 9; dan++) {
            for (times = 1; times <= 9; times++) {
                System.out.println(dan + "X" + times + "=" + dan * times);
            }
            System.out.println();
        }
    }
}

별찍기

package Day02;

public class Q2 {
    static void main() {
        /*
        1.
        i = 0(개행)
        j = 0(*) 1(*) 2(*) 3(*) 4(*)
        i = 1(개행)
        j = 0(*) 1(*) 2(*) 3(*) 4(*)
        i = 2(개행)
        j = 0(*) 1(*) 2(*) 3(*) 4(*)
        i = 3(개행)
        j = 0(*) 1(*) 2(*) 3(*) 4(*)
        i = 4(개행)
        j = 0(*) 1(*) 2(*) 3(*) 4(*)
         */

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

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

        /*
        3.
        i = 0(개행)
        j = 0(a) 1(a) 2(a) 3(a) 4(*)
        i = 1(개행)
        j = 0(a) 1(a) 2(a) 3(*) 4(*)
        i = 2(개행)
        j = 0(a) 1(a) 2(*) 3(*) 4(*)
        i = 3(개행)
        j = 0(a) 1(*) 2(*) 3(*) 4(*)
        i = 4(개행)
        j = 0(*) 1(*) 2(*) 3(*) 4(*)
         */
        System.out.println(3);
        for (int i = 0; i < 5; i++) {
            for (int j = 4; j >= 0; j--) {
                if (i<j) {
                    System.out.print("  ");
                }
                else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }

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

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

    }
}

랜덤 문제

package Day02;

import java.util.Random;

public class Q3 {
    static void main() {
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.print((char) ('A' + r.nextInt(26)));
        }
    }
}

마무리

오늘은 선생님이 바뀌시면서 java를 처음부터 다시 찝어주셨다.
확실히 전 선생님보다 훨씬 이해하기 쉬웠고 수업이 재밌었다.
이것저것 배우지 않았던 것들까지 배우면서 머리가 조금 복잡해지긴 했지만 열심히 공부하면 충분히 할 수 있을 거 같다.

0개의 댓글