[인프런 워밍업 클럽 스터디 1기 과제 #5] 클린 코드

qk·2024년 5월 9일
post-thumbnail

문제 1

import java.util.Scanner;

public class Main {
    public static int numOfFaces = 6; // 주사위 숫자의 범위에 변경이 있을 경우 변경을 최소화한다.

    public static void main(String[] args) {
        int times = EnterTimes();
        int[] counts = rollDice(times);
        print(counts);
    }

    // 사용자로부터 주사위를 던지는 횟수를 입력받는다.
    private static int EnterTimes() {
        System.out.print("숫자를 입력하세요: ");
        Scanner scanner = new Scanner(System.in);
        int times = scanner.nextInt();
        return times;
    }

    // 주사위를 던져 나오는 숫자를 랜덤으로 생성한다.
    private static int[] rollDice(int times) {
        int[] counts = new int[numOfFaces+1];
        for (int i = 0; i < times; i++) {
            double randomNumber = Math.random() * numOfFaces;
            int face = (int) (randomNumber+1);
            counts[face]++;
        }
        return counts;
    }

    // 결과를 출력한다.
    private static void print(int[] counts) {
        for (int i = 1; i < counts.length; i++) {
            System.out.printf("%d은 %d번 나왔습니다.\n", i, counts[i]);
        }
    }
}

* 주사위 면의 범위가 달라졌을 경우

출처

자바와 스프링 부트로 생애 최초 서버 만들기, 누구나 쉽게 개발부터 배포까지! [서버 개발 올인원 패키지]

0개의 댓글