로또 당첨 프로그램

- 수행 목적 : Scanner의 입력함수와 조건문 및 반복문과 배열을 통한 로또 당첨 로직 작성

- 간략 소개 : 로또는 1-45개의 숫자 사이의 값 중 6개를 맞추면 당첨되는 복권입니다. 

로또의 개수를 구매하고(구매수량 입력), 당첨번호를 생성한다. 이후, 구매한 로또의 당첨번호를 판단하는 프로그램을 작성해 보세요.

 

※ 필수 준수사항

1. 로또 구매 수량 입력

2. 입력한 개수만큼의 로또 개수 생성

3. 로또 당첨 번호 생성

4. 당첨 번호와 구매 로또 비교하여 숫자 일치 여부 판단


Random 함수

java.util 패키지 중 하나인 Random은 난수 발생기이다. 

일정 범위가 주어지면 그 사이의 수 중 하나를 임의로 선택하게 된다.

nextInt() 매서드를 많이 쓰는데 범위를 따로 지정해주지 않으면 int의 광범위한 정수 범위 중 하나의 숫자를 골라주니 보통은 범위를 넣게 된다.

 

ex) int a = random.nextInt(10) // 0부터 10개의 정수 중 하나를 랜덤으로 추출

0부터 출력되기 때문에 로또 번호 랜덤 출력 시 아래와 같이 추출을 원하는 정수에서 +1 (즉, 1~45 중 랜덤 출력)

 

for (int i = 0; i < 6; i++) { // 랜덤번호 생성
    lotto[a][i] = rnd.nextInt(45) + 1;

작성 코드

/*
    로또 당첨 프로그램    
    아는 함수 선에서 코드를 작성하다보니 상대적으로 시간이 많이 소요되었습니다.
 */

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

public class Lotto {
    public static void main(String[] args) {

        int[][] lotto = new int[10][6]; // 사용자 로또 번호 배열
        Scanner sc = new Scanner(System.in);
        char[] alphabet = {'A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E'};
        int[] reallotto = new int[6]; // 로또 정답 배열
        Random rnd = new Random();

        while (true) {
            System.out.println("[로또 당첨 프로그램]");
            System.out.println();
            System.out.print("로또 개수를 입력해 주세요.(숫자 1~10): ");
            int input = sc.nextInt();

            if (input < 1 || input > 10) {
                System.out.println("숫자 1~10 중 입력해 주세요.");
                System.out.println("--------------------------");
                continue;
            } else {
                for (int a = 0; a < input; a++) {
                    System.out.printf("%c\t", alphabet[a]);
                    for (int i = 0; i < 6; i++) { // 랜덤번호 생성
                        lotto[a][i] = rnd.nextInt(45) + 1;
                        for (int j = 0; j < i; j++) { // 중복 값 제거
                            if (lotto[a][i] == lotto[a][j]) {
                                i--;
                            }
                        }
                    }
                    for (int c = 0; c <= 5; c++) { // 당첨번호 생성
                        reallotto[c] = rnd.nextInt(45) + 1;
                        for (int d = 0; d < c; d++) {
                            if (reallotto[d] == reallotto[c]) {
                                c--;
                            }
                        }
                    }
                    for (int i = 0; i<lotto[a].length-1; i++) { // 사용자 로또 번호 오름차순 정렬
                        for (int j = i + 1; j < lotto[a].length; j++) {
                            if (lotto[a][i] > lotto[a][j]) {
                                int temp = lotto[a][j];
                                lotto[a][j] = lotto[a][i];
                                lotto[a][i] = temp;
                            }
                        }
                    }
                    for (int i = 0; i < 6; i++) { // 사용자 로또 번호 출력
                        System.out.printf("%02d", lotto[a][i]);
                        if (i < 5) {
                            System.out.print(",");
                        }
                    }
                    System.out.println();
                }
                System.out.println();
            }
            System.out.println("[로또 발표]");
            System.out.print(" \t");

            for (int c = 0; c < 6; c++) { // 당첨 로또 번호 오름차순 정렬
                for (int d = c + 1; d < 6; d++) {
                    if (reallotto[c] > reallotto[d]) {
                        int temp2 = reallotto[c];
                        reallotto[c] = reallotto[d];
                        reallotto[d] = temp2;
                    }
                }
            }
            for (int c = 0; c < 6; c++) { // 당첨 로또 번호 출력
                System.out.printf("%02d", reallotto[c]);
                if (c < 5) {
                    System.out.print(",");
                }
            }
            System.out.println("\n");
            System.out.println("[내 로또 결과]");

            for (int a = 0; a < input; a++) {
                System.out.printf("%c\t", alphabet[a]);
                int count = 0;
                for (int i = 0; i < 6; i++) {
                    System.out.printf("%02d", lotto[a][i]);
                    if (i < 5) {
                        System.out.print(",");
                    }
                }
                for (int j : reallotto) {
                    for (int i = 0; i < 6; i++) {
                        if (j == lotto[a][i]) {
                            count++; // 사용자 로또 배열 값이 로또 당첨 배열 값과 같으면 count 증가
                        }
                    }
                }
                System.out.printf(" => %d개 일치", count);
                System.out.println();
            }
            System.out.println();
            break;
        }
    }
}

https://gist.github.com/coha96/2895cd9772d40b2676f32cdd1c64b858