본문 바로가기
Programming/JAVA

자바 로또 당첨 확인

by 추천캐릭터 2022. 6. 18.
728x90

class lotto1 {
    private final static int[] lottoResult = new int[6];
    /**
     * 이번주 로또 1등번호
     */
    public static int[] lottoResult() {
        for (int i = 0; i < lottoResult.length; i++) {
            lottoResult[i] = (int) ((Math.random() * 45) + 1);
            // 동일 번호 확인
            for (int j = 0; j < i; j++) {
                if (lottoResult[i] == lottoResult[j]) {
                    i--;
                }
            }
        }
        // 번호 정렬
        numSort(lottoResult, 0);
        for (int i = 0; i < lottoResult.length; i++) {
            System.out.println("이번주 1등 번호 = " + lottoResult[i]);
        }
        return lottoResult;
    }


    private static int[] purchaseNum = new int[6];
    /**
     * 구매한 로또 번호
     */
    public static int[] purchaseLotto() {
        for (int i = 0; i < purchaseNum.length; i++) {
            purchaseNum[i] = (int) ((Math.random() * 45) + 1);
            for (int j = 0; j < i; j++) {
                if (purchaseNum[i] == purchaseNum[j]) {
                    i--;
                }
            }
        }

// 일반 오름차순 정렬

Arrays.sort(purchaseNum);


        // 직접 구현한 버블 소트 번호 정렬 - 성능상 젤 구림
        numSort(purchaseNum, 0);
        for (int i = 0; i < purchaseNum.length; i++) {
            System.out.println("구매한 로또 번호 = " + purchaseNum[i]);
        }
        return purchaseNum;
    }


    /**
     * 이번 로또 몇개 사면 됬을까?
     */
    public static void resultCheck() {
        int[] oriNum = lottoResult;
        Integer cnt = 0;
        while (true) {
            Integer cn = cnt++;
            int[] purNum = purchaseLotto();
            boolean equals = Arrays.equals(oriNum, purNum);
            System.out.println(cn);
            if (equals == true) {
                break;
            }
        }
    }


    /**
     * 아래 두 메소드는 1차원 정렬 메소드

    * 버블 소트 정렬  성능상 젤 구림

     */
    public static void numSort(int[] arr, int start) {
        if (start < arr.length - 1) {
            int min_index = start;
            for (int i = start; i < arr.length; i++) {
                if (arr[i] < arr[min_index]) {
                    min_index = i;
                }
            }
            swap(arr, start, min_index);
            numSort(arr, start + 1);
        }
    }


    public static void swap(int[] arr, int index1, int index2) {
        int tmp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = tmp;
    }
}

728x90

댓글