한국어 깨짐

이가영·2024년 10월 27일
0

인텔리제이에서 입력을 한국어로 작성하면 글자가 깨져서 영어로 작성함. -> 낼 때 영어로 작성해서 냄

우테코 프리코스 진짜 답(한국어로).

package racingcar;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Application {
    public static void main(String[] args) {
        //Todo 프로그램 구현
        Scanner scanner = new Scanner(System.in);

        try {
            System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
            String inputNames = scanner.nextLine();
            List<String> carNames = validateCarNames(inputNames);

            System.out.println("시도할 횟수는 몇 회인가요?");
            int moves = Integer.parseInt(scanner.nextLine());

            Racing racing = new Racing(carNames);
            racing.startRace(moves);
            List<String> winners = racing.getWinners();
            System.out.println("최종 우승자 : " + String.join(", ", winners));

        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }

    private static List<String> validateCarNames(String inputNames) {
        List<String> carNames = Arrays.stream(inputNames.split(","))
                .map(String::trim)
                .collect(Collectors.toList());

        if (carNames.isEmpty() || carNames.stream().anyMatch(name -> name.length() > 5)) {
            throw new IllegalArgumentException("자동차 이름은 5자 이하로 입력해야 합니다.");
        }

        return carNames;
    }
}

package racingcar;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class Racing {
    private final List<Car> cars;
    private static final int FORWARD_THRESHOLD = 4;
    private final Random random;
    private boolean isFirstRound = true; // 첫 번째 라운드인지 확인하는 변수

    public Racing(List<String> carNames) {
        this.cars = carNames.stream()
                .map(Car::new)
                .collect(Collectors.toList());
        this.random = new Random();
    }

    public void startRace(int moves) {
        for (int i = 0; i < moves; i++) {
            for (Car car : cars) {
                if (random.nextInt(10) >= FORWARD_THRESHOLD) {
                    car.move();
                }
            }
            displayRaceStatus();
        }
    }

    private void displayRaceStatus() {
        if (isFirstRound) {
            System.out.println("실행 결과");
            isFirstRound = false;
        }

        for (Car car : cars) {
            System.out.println(car.getName() + " : " + "-".repeat(car.getPosition()));
        }
        System.out.println();
    }

    public List<String> getWinners() {
        int maxPosition = cars.stream()
                .mapToInt(Car::getPosition)
                .max()
                .orElse(0);

        return cars.stream()
                .filter(car -> car.getPosition() == maxPosition)
                .map(Car::getName)
                .collect(Collectors.toList());
    }

    private static class Car {
        private final String name;
        private int position;

        public Car(String name) {
            this.name = name;
            this.position = 0;
        }

        public String getName() {
            return name;
        }

        public int getPosition() {
            return position;
        }

        public void move() {
            position++;
        }
    }
}

pwd

src/main/java/racingcar/Application.java
src/main/java/racingcar/Racing.java

profile
gy’s portfolio

0개의 댓글