Last active
August 2, 2023 03:42
-
-
Save kingseungil/8369e5add1be3d8050a7266285ae77bb to your computer and use it in GitHub Desktop.
Zerobase-miniproject
This file contains 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.ArrayList; | |
import java.util.Collections; | |
import java.util.Random; | |
import java.util.Scanner; | |
public class Miniproject7 { | |
public static void main(String[] args) { | |
System.out.println("[로또 당첨 프로그램]\n"); | |
try (Scanner sc = new Scanner(System.in)) { | |
System.out.print("로또 개수를 입력해 주세요.(숫자 1 ~ 10):"); | |
int lottoCount = sc.nextInt(); | |
ArrayList<ArrayList<Integer>> lottoList = new ArrayList<>(); // ArrayList 사용 | |
// 로또번호 생성 | |
for (int i = 0; i < lottoCount; i++) { | |
lottoList.add(generateLottoNumber()); // 메소드 호출 | |
} | |
// 로또번호 출력 | |
char order = 'A'; | |
for (ArrayList<Integer> lotto : lottoList) { | |
System.out.print(order++ + "\t\t\t"); | |
for (int i = 0; i < lotto.size(); i++) { | |
if (i == 5) { | |
System.out.format("%02d", lotto.get(i)); | |
} else { | |
System.out.format("%02d,", lotto.get(i)); | |
} | |
} | |
System.out.println(); | |
} | |
// 당첨번호 생성 | |
ArrayList<Integer> winningNumber = generateLottoNumber(); // 메소드 호출 | |
// 당첨번호 출력 | |
System.out.println("\n[로또 발표]"); | |
System.out.print("\t\t\t"); | |
for (int i = 0; i < winningNumber.size(); i++) { | |
if (i == 5) { | |
System.out.format("%02d", winningNumber.get(i)); | |
} else { | |
System.out.format("%02d,", winningNumber.get(i)); | |
} | |
} | |
// 당첨여부 확인 | |
int[] result = new int[lottoCount]; | |
for (int i = 0; i < lottoList.size(); i++) { | |
for (int j = 0; j < lottoList.get(i).size(); j++) { | |
if (winningNumber.contains(lottoList.get(i).get(j))) { // contains 메소드 사용 | |
result[i]++; | |
} | |
} | |
} | |
// 당첨여부 출력 | |
System.out.println("\n"); | |
System.out.println("[내 로또 결과]"); | |
order = 'A'; | |
for (int i = 0; i < lottoCount; i++) { | |
System.out.print(order++ + "\t\t\t"); | |
for (int j = 0; j < lottoList.get(i).size(); j++) { | |
if (j == 5) { | |
System.out.format("%02d => ", lottoList.get(i).get(j)); | |
} else { | |
System.out.format("%02d,", lottoList.get(i).get(j)); | |
} | |
} | |
System.out.println(result[i] + "개 일치"); | |
} | |
} | |
} | |
// 로또번호 생성 메소드 | |
public static ArrayList<Integer> generateLottoNumber() { | |
ArrayList<Integer> lottoNumber = new ArrayList<>(); | |
Random random = new Random(); // Random 객체 생성 | |
while (lottoNumber.size() < 6) { | |
int num = random.nextInt(45) + 1; // 0 ~ 44 사이의 정수에 1을 더함 | |
if (!lottoNumber.contains(num)) { // contains 메소드 사용 | |
lottoNumber.add(num); | |
} | |
} | |
Collections.sort(lottoNumber); // sort 메소드 사용 | |
return lottoNumber; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment