내배캠 사전캠프 TIL 8일차

오병택·2025년 1월 22일

내배캠

목록 보기
7/73

학습 요약

복습, SQL 달리기 3번까지, 자바 달리기 2번까지

SQL

기초 문법

ROUND(숫자, 소수점 아래 반올림할 자릿 수) // 반올림

JAVA

달리기 1번: 랜덤 닉네임 생성기

import java.util.Random;

public class run1 {
    public static void main(String[] args) {
        String [] arr1 = {"기절초풍", "멋있는", "재미있는"};
        String [] arr2 = {"도전적인", "노란색의", "바보같은"};
        String [] arr3 = {"돌고래", "개발자", "오랑우탄 "};

        Random random = new Random(); // 랜덤 객체 생성
        int index1 = random.nextInt(arr1.length); 
        int index2 = random.nextInt(arr2.length);
        int index3 = random.nextInt(arr3.length); // 랜덤 인덱스 생성 (0부터 list1.length-1까지)

        String a1 = arr1[index1];
        String a2 = arr2[index2];
        String a3 = arr3[index3];
        System.out.println("닉네임: "+a1+a2+a3);
    }
}

달리기 2번: 스파르타 자판기

import java.util.Map;
import java.util.Scanner;

public class run2 {
    public static void main(String[] args) {
        Map<String, Integer> beverages = Map.of(
                "사이다", 1700,
                "콜라", 1900,
                "식혜", 2500,
                "솔의눈", 3000); // Map 객체 생성하고 값을 지정

        for (Map.Entry<String, Integer> beverage : beverages.entrySet()) {
            System.out.println(beverage.getKey() + " " + String.format("%,d원", beverage.getValue()));
        } // map.entrySet()는 Map에 포함된 키와 값 쌍을 Set 형태로 반환, Map.Entry<K, V>는 Map.Entry 객체

        System.out.println("어떤 음료를 살 것 인가요?");
        Scanner scanner = new Scanner(System.in);
        String userChoice = scanner.nextLine();

        if (beverages.containsKey(userChoice)) { // 키 포함 여부
            System.out.println(userChoice + "를 고르셨습니다.");
        } else {
            System.out.println("목록에 있는 음료 중에서 골라주세요: " + beverages.keySet());  // Map에 저장된 모든 키를 Set 컬렉션 형태로 반환
            System.exit(0); // 시스템 종료
        }
        int coin = scanner.nextInt();
        int balance = coin - beverages.get(userChoice);
        if (coin >= beverages.get(userChoice)) {
            System.out.println(coin + "원 받았습니다");
            System.out.println("남은 잔액: "+ balance);
        } else {
            System.out.println("돈이 부족합니다.");
        }

    }
}

느낀 점

SQL 달리기4번 살짝 봤는데 어지러워서 자바 달리기 문제로 도망갔다. 다행히 SQL보다는 할만 한 것 같다. 자바 빨리 다 하고 SQL 다시 해야 겠다.

profile
걱정하지 말고 일단 해봐!

0개의 댓글