Created
February 1, 2025 14:43
-
-
Save Exom9434/53924f5e6719e8956f742c9e29533839 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //노재경 | |
| import java.util.Random; | |
| import java.util.Scanner; | |
| public class Main { | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| // 1) 구매할 로또 개수 입력 | |
| System.out.print("구매할 로또 개수를 입력해주세요.(숫자1~10): "); | |
| int count = sc.nextInt(); | |
| // 개수 체크 | |
| while (count < 1 || count > 10) { | |
| System.out.println("1에서 10 사이의 값을 입력해주세요."); | |
| System.out.print("구매할 로또 개수를 입력해주세요.(숫자 1~10): "); | |
| count = sc.nextInt(); | |
| } | |
| // 2) 당첨 번호 생성 | |
| int[] winningNumbers = createNumbersList(); | |
| // 3) 당첨 번호 출력 | |
| System.out.println("[로또발표]"); | |
| for (int num : winningNumbers) { | |
| System.out.print(num + " "); | |
| } | |
| System.out.println(); | |
| // 4) 구매 개수만큼 티켓 생성 & 당첨 결과 확인 | |
| System.out.print("[내 로또 결과]"); | |
| System.out.println(); | |
| for (int i = 1; i <= count; i++) { | |
| int[] ticket = createNumbersList(); | |
| int matchCount = getMatchCount(winningNumbers, ticket); | |
| // 티켓id값 생성: | |
| char ticketLabel = (char) ('A' + (i - 1)); | |
| // 결과출력 | |
| System.out.print(ticketLabel + " "); | |
| for (int num : ticket) { | |
| System.out.print(num + " "); | |
| } | |
| System.out.println("=> " + matchCount + "개 일치"); | |
| } | |
| sc.close(); | |
| } | |
| // 로또 번호 생성 | |
| private static int[] createNumbersList() { | |
| int[] numbers = new int[6]; | |
| Random random = new Random(); | |
| int index = 0; | |
| while (index < 6) { | |
| int newNum = random.nextInt(45) + 1; // 1 ~ 45 범위 | |
| boolean isDuplicate = false; | |
| // 이미 뽑은 숫자 중에 같은 것이 있는지 확인하는 파트 | |
| for (int i = 0; i < index; i++) { | |
| if (numbers[i] == newNum) { | |
| isDuplicate = true; | |
| break; | |
| } | |
| } | |
| //중복이 아니면 리스트에 추가 | |
| if (!isDuplicate) { | |
| numbers[index] = newNum; | |
| index++; | |
| } | |
| } | |
| return numbers; | |
| } | |
| // 일치하는 것 체크하는 함수-> element별 구성성 | |
| private static int getMatchCount(int[] arr1, int[] arr2) { | |
| int count = 0; | |
| for (int num1 : arr1) { | |
| for (int num2 : arr2) { | |
| if (num1 == num2) { | |
| count++; | |
| } | |
| } | |
| } | |
| return count; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment