T_era

[JAVA] 문제풀이 : 단어 퀴즈 만들기 본문

Programing/Java

[JAVA] 문제풀이 : 단어 퀴즈 만들기

블스뜸 2025. 3. 26. 16:50
1.
컴퓨터가 랜덤으로 영어단어를 선택합니다.
a.
영어단어의 자리수를 알려줍니다.
ex ) PICTURE = 7자리 => _ _ _ _ _ _ _
String word = wordQuiz.wordList[rand.nextInt(wordQuiz.wordList.length)];
int wordLength = word.length();
String str = wordQuiz.TextLength(wordLength);;
System.out.println(str);
2.
사용자는 A 부터 Z 까지의 알파벳 중에서 하나를 입력합니다.
a.
입력값이 A-Z 사이의 알파벳이 아니라면 다시 입력을 받습니다
b.
입력값이 한 글자가 아니라면 다시 입력을 받습니다
c.
이미 입력했던 알파벳이라면 다시 입력을 받습니다.
d.
입력값이 정답에 포함된 알파벳일 경우 해당 알파벳이 들어간 자리를 전부 보여주고, 다시 입력을 받습니다.
i.
ex ) 정답이 eyes 인 경우에 E 를 입력했을 때
1.
_ _ _ _ => e_e_
e.
입력값이 정답에 포함되지 않은 알파벳일 경우 기회가 하나 차감되고, 다시 입력을 받습니다.
Scanner sc = new Scanner(System.in);

        while (true) {
            String msg = "";
            String str = strEmpty;
            boolean successFlag = false;

            System.out.print("입력 : ");
            msg = sc.nextLine().toLowerCase();
			// 2-a, 2-b
            if (msg.length() > 1) {
                System.out.println("한글자만 입력해 주세요");
                continue;
            } else if(msg.isEmpty()){
                System.out.println("입력된 값이 없습니다.");
                continue;
            } else if (!Character.isLetter(msg.charAt(0))) {
                System.out.println("영문자를 입력해주세요");
                continue;
            }
			// 2-c
            boolean setflag = false;
            for (int i = 0; i < writeList.size(); i++) {
                if (writeList.get(i) == msg.charAt(0)) {
                    System.out.println("이미 입력한 문자입니다");
                    setflag = true;
                }
            }
            if (setflag) continue;
			// 2-d, 2-e
            for (int i = 0; i < _word.length(); i++) {
                if (_word.charAt(i) == msg.charAt(0)) {
                    successFlag = true;
                    break;
                }
            }
            
        }
3.
사용자가 9번 틀리면 게임오버됩니다.
			// 3번문제
             else {
            	
                overCount--;
                System.out.println("틀렸습니다");
            }
            if (overCount <= 0) {
                System.out.println("게임오버");
                System.out.println("정답은 : " + _word);
                break;
            }
4.
게임오버 되기 전에 영어단어의 모든 자리를 알아내면 플레이어의 승리입니다.
			// 4번문제
            if (successFlag) {
                writeList.add(msg.charAt(0));

                for(int i = 0; i < _word.length(); i++){
                    for(Character c : writeList){
                        if(_word.charAt(i) == c && str.charAt(i) == '_'){
                            str = str.substring(0, i) + c + str.substring(i+1);
                        }
                    }
                }
                System.out.println(str);
                if(str.equals(_word)) {
                    System.out.println("클리어!");
                    break;
                }
			
            }

전체 코드

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

public class WordQuiz {
    private final String[] wordList = {"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"};
    private final ArrayList<Character> writeList = new ArrayList<Character>();
    private int overCount = 9;

    public static void main(String[] args) {
        WordQuiz wordQuiz = new WordQuiz();

        Random rand = new Random();
        rand.setSeed(System.currentTimeMillis());

        // 1번문제 랜덤단어와 단어의 길이
        String word = wordQuiz.wordList[rand.nextInt(wordQuiz.wordList.length)];
        int wordLength = word.length();
        String str = wordQuiz.TextLength(wordLength);;
        System.out.println(str);
        // 2번문제 문자 입력해서 빈칸 채우기
        wordQuiz.WriteCharacter(word, str);
    }

    public String TextLength(int length) {
        System.out.println(length);
        String str = "";
        for (int i = 0; i < length; i++) {
            str += "_";
        }
        return str;
    }

    public void WriteCharacter(String _word, String strEmpty) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            String msg = "";
            String str = strEmpty;
            boolean successFlag = false;

            System.out.print("입력 : ");
            msg = sc.nextLine().toLowerCase();

            if (msg.length() > 1) {
                System.out.println("한글자만 입력해 주세요");
                continue;
            } else if(msg.isEmpty()){
                System.out.println("입력된 값이 없습니다.");
                continue;
            } else if (!Character.isLetter(msg.charAt(0))) {
                System.out.println("영문자를 입력해주세요");
                continue;
            }

            boolean setflag = false;
            for (int i = 0; i < writeList.size(); i++) {
                if (writeList.get(i) == msg.charAt(0)) {
                    System.out.println("이미 입력한 문자입니다");
                    setflag = true;
                }
            }
            if (setflag) continue;

            for (int i = 0; i < _word.length(); i++) {
                if (_word.charAt(i) == msg.charAt(0)) {
                    successFlag = true;
                    break;
                }
            }
            if (successFlag) {
                writeList.add(msg.charAt(0));

                for(int i = 0; i < _word.length(); i++){
                    for(Character c : writeList){
                        if(_word.charAt(i) == c && str.charAt(i) == '_'){
                            str = str.substring(0, i) + c + str.substring(i+1);
                        }
                    }
                }
                System.out.println(str);
                if(str.equals(_word)) {
                    System.out.println("클리어!");
                    break;
                }

            } else {
                overCount--;
                System.out.println("틀렸습니다");
            }
            if (overCount <= 0) {
                System.out.println("게임오버");
                System.out.println("정답은 : " + _word);
                break;
            }
        }
    }
}