내배캠 사전캠프 TIL 10일차

오병택·2025년 1월 24일

내배캠

목록 보기
9/73

학습 요약

자바 달리기 다 하기

SQL

JAVA

자바 달리기 3번: 단어 맞추기 게임

import java.util.Random;
import java.util.Scanner;

public class run3 {
    public static void main(String[] args) {
        String[] arr = {
                "airplane","apple","arm","bakery","banana","bank","bean","belt","bicycle","biography",
                "blackboard","boat","bowl","broccoli","bus","car", "carrot","chair","cherry","cinema",
                "class","classroom","cloud","coat","cucumber","desk","dictionary",
                "dress","ear","eye","fog","foot","fork","fruits","hail","hand","head","helicopter",
                "hospital","ice","jacket","kettle","knife","leg","lettuce","library","magazine",
                "mango","melon","motorcycle","mouth","newspaper","nose","notebook","novel",
                "onion","orange","peach","pharmacy","pineapple","plate","pot","potato","rain","shirt","shoe",
                "shop","sink","skateboard","ski","skirt","sky","snow","sock","spinach","spoon","stationary",
                "stomach","strawberry","student","sun","supermarket","sweater","teacher","thunderstorm",
                "tomato","trousers","truck","vegetables","vehicles","watermelon","wind"
        };
        int life = 9;
        Random random = new Random();
        String wordanswer = arr[random.nextInt(arr.length)];
        System.out.println("자리수 :" + wordanswer.length() + "글자");
        Scanner scanner = new Scanner(System.in);
        char[] display = new char[wordanswer.length()];
        for (int i = 0; i < display.length; i++) {
            display[i] = '_';
        }                                             // display를 _로 초기화
        System.out.println(display);
        boolean[] guessedLetters = new boolean[26]; // 알파벳 26개 true, false로 반복됐는지 구분
        while (life != 0) {
            System.out.println("");
            System.out.println("알파벳을 입력해주세요");
            String wor = scanner.next();
            String word = wor.toLowerCase();
            char charword = word.charAt(0);
            if (guessedLetters[charword - 'a']) {           // 아스키 코드를 이용하여 인덱스 값으로 사용
                System.out.println("이미 입력한 문자입니다");
                continue;                                  
            }if (!Character.isLetter(charword)) {        // 알파벳인지 구분
                System.out.println("문자가 아닙니다");
                continue;
            } else if (word.length() > 1) {
                System.out.println("한글자만 적어주세요.");
                continue;
            }
            guessedLetters[charword - 'a'] = true;      // 반복됐으면 true
            if (!wordanswer.contains(word)) {
                System.out.println("안 들어있습니다");
                life -= 1;
                System.out.println("남은 목숨: " + life);
            } else if (wordanswer.contains(word)) {
                System.out.println("들어있습니다");
                int index = wordanswer.indexOf(word);
                while (index != -1) {
                    display[index] = charword;
                    index = wordanswer.indexOf(word, index + 1);
                }                                             // 문자가 들어있으면 display에 추가 
                System.out.println(display);
                if (String.valueOf(display).equals(wordanswer)) {
                    System.out.println("you win!!!!!");
                    break;
                }
                continue;
            }
        }
        if (life == 0) {
            System.out.println("you failed.");
        }

    }
}

자바 달리기 보너스 문제: 가위 바위 보

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class run4 {
    public static void main(String[] args) {
        int count = 5;
        int wincount=0;
        String[] com = { "가위", "바위", "보" };
        Scanner sc = new Scanner(System.in);
        Random rd = new Random();

        Map<Integer, String> gifts = new HashMap<>();
        gifts.put(0, "꽝");
        gifts.put(1, "곰돌이 인형");
        gifts.put(2, "스파르타 랜드 입장권");
        gifts.put(3, "스파르타 캐니언 항공 투어권");
        gifts.put(4, "호텔 스파르타 숙박권");
        gifts.put(5, "스파르테이트 항공권"); // map을 사용하여 키 값쌍으로 저장

        while (count != 0) {
            String rsp = com[rd.nextInt(com.length)];
            System.out.println("가위 바위 보 중 하나를 입력해주세요!");
            String user = sc.nextLine();
            if (user.equals("가위")) {
                if (rsp.equals("가위")) {
                    System.out.println("당신은 가위 그리고 상대는 가위로 서로 비겼습니다");
                } else if (rsp.equals("바위")) {
                    System.out.println("당신은 가위 그리고 상대는 바위로 당신이 졌습니다");
                } else {
                    System.out.println("당신은 가위 그리고 상대는 보로 당신이 이겼습니다");
                    wincount+=1;

                }
                count -= 1;
            } else if (user.equals("바위")) {
                if (rsp.equals("가위")) {
                    System.out.println("당신은 바위 그리고 상대는 가위로 당신이 이겼습니다");
                    wincount+=1;
                } else if (rsp.equals("바위")) {
                    System.out.println("당신은 바위 그리고 상대는 바위로 서로 비겼습니다");
                } else {
                    System.out.println("당신은 바위 그리고 상대는 보로 당신이 졌습니다");
                }
                count -= 1;
            } else if (user.equals("보")) {
                if (rsp.equals("가위")) {
                    System.out.println("당신은 보 그리고 상대는 가위로 당신이 졌습니다");
                } else if (rsp.equals("바위")) {
                    System.out.println("당신은 보 그리고 상대는 바위로 당신이 이겼습니다");
                    wincount+=1;
                } else {
                    System.out.println("당신은 보 그리고 상대는 보로 서로 비겼습니다");
                }
                count -= 1;
            } else {
                System.out.println("잘못된 입력입니다");
                continue;
            }
        }
        System.out.println("축하합니다! 총 "+ wincount+"회 승리하여 "+gifts.get(wincount)+"를 획득하셨습니다.");
    }
}

느낀 점

비주얼 스튜디오 코드 콘솔에서 한글 입력 못 받는 것 때문에 인텔리제이 커뮤니티 버전도 같이 썼다. 그리고 컨디션 관리를 잘해야 겠다. 피곤하면 어제처럼 이해가 잘 안 되는데 오늘은 잠을 많이 자서 이해가 잘 됐다.

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

0개의 댓글