본문 바로가기
Project/연습문제풀이

[JAVA] Lotto 번호 생성기

by 8Antony 2022. 6. 13.

Lotto

 

로또 번호를 생성후 --> 당첨 여부를 확인하는 코드를 짜 보았다.

 

idea

 

1. 은행이 고른 당첨번호를 만든다. (while, Mathrandom)

2. player가 고른 당첨 번호를 만든다. (scanner, nextint)

3. 당첨번호를 저장한다. (boolean)

4. 등수 뽑은 후 비교 (if)

 

import java.util.Arrays;
import java.util.Scanner;

public class LottoExam {

	public static void main(String[] args) {

		//
		int[] lotto = new int[6];
		int[] player = new int[6];
		// 보너스 번호
		int bonusNum = 0;

		// 은행이 고른 당첨번호 만들기
		int count = 0; // 로또 개수 카운트 변수

		while (count < 7) {

			// 중복 체크 중복이면 temp 0으로 바꾼다.
			int temp = (int) (Math.random() * 45) + 1;

			for (int i = 0; i < count; i++) {
				if (temp == lotto[i]) {
					temp = 0; // temp가 0이 아닐때만 삽입
					break;
				}
			}

			// temp가 0 보다 커야 중복이 아니다.
			if (temp > 0) {
				// 6번 뽑으면 공을 보너스 번호에 넣는다.
				if (count < 6) {
					lotto[count] = temp;
				} else {
					bonusNum = temp;
				}
				// 카운트 증가
				count++;
			}
		}

		System.out.println("로또번호 : " + Arrays.toString(lotto) + ", 보너스 번호 :" + bonusNum);

		Scanner scan = new Scanner(System.in);

		// 사용자 번호 만들기
		for (int i = 0; i < 6; i++) {
			System.out.println((i + 1) + " 번째 로또 번호 >>>>");

			player[i] = scan.nextInt();

			if (player[i] < 1 || player[i] > 45) {
				System.out.println("잘못입력했습니다. 1부터 45사이에서 선택하세요.");
				i--;
				continue; // 여기서 이번 반복 종료 하고 다시!
			}

			// 중복 체크 (이전의 값들과 비교하는 것 = j
			for (int j = 0; j < i; j++) {
				if (player[i] == player[j]) {
					System.out.println("이미 선택한 번호 입니다. 다른 번호를 선택하세요.");
					i--;
					break;
				}
			}
		}

		// 스캐너 닫기
		scan.close();

		System.out.println("사용자선택 번호 : " + Arrays.toString(player));

		// 당첨번호를 저장할 배열을 만든다.
		int[] matchNumbers = new int[6];
		// 보너스 번호 매치 여부
		boolean bonusMatched = false;
		int matchCount = 0; // 맞은 갯수

		for (int i = 0; i < player.length; i++) {
			for (int j = 0; j < lotto.length; j++) {

				// 내번호를 기준으로 당첨번호와 맞는 것이 있는지 찾는다.
				if (player[i] == lotto[j]) {
					matchNumbers[matchCount++] = player[i];
					break; // 찾았으니 그만
				}
			}

			// 보너스 번호를 못찾았을 경우만 비교 하게 하면 비교횟수를 줄일 수 있다.
			if (!bonusMatched) {
				if (player[i] == bonusNum) {
					bonusMatched = true;
				}
			}
		}

		// println 이 아니라 print()로 하는 이유는 보너스번호가 있으면 뒤에 쓰고 줄을 바꾸고
		// 만약 보너스 번호가 없으면 그냥 줄을 바꾸기 위해 우선 줄바꿈 없이 출력
		System.out.print("당첨 번호 : " + Arrays.toString(matchNumbers));

		if (matchCount == 5 && bonusMatched) {
			System.out.println(", 보너스 번호 : " + bonusNum);
		} else {
			// 그냥 줄바꿈
			System.out.println();
		}

		// 등수를 출력하자
		if (matchCount == 6) {
			System.out.println("1등입니다.");
		} else if (matchCount == 2 && bonusMatched) {
			System.out.println("2등입니다.");
		} else if (matchCount == 3 && bonusMatched) {
			System.out.println("3등입니다.");
		} else if (matchCount == 4 && bonusMatched) {
			System.out.println("4등입니다.");
		} else {
			System.out.println("5등입니다.");
		}

	}

}

 

배열에 저장을 했으면 꼭 System.out.println(Arrays.toString(배열)); 으로 출력하자

'Project > 연습문제풀이' 카테고리의 다른 글

[Oracle] 연습문제 풀이  (0) 2022.06.24

댓글