Last active
January 15, 2024 08:03
-
-
Save Starlight258/832935661427a1ee3cba62ca61c4489f to your computer and use it in GitHub Desktop.
0114_과제7
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.*; | |
public class a7 { | |
public static void main(String[] args) { | |
// 입력받기 | |
System.out.println("[로또 당첨 프로그램]\n"); | |
Scanner sc = new Scanner(System.in); | |
System.out.print("로또 개수를 입력해 주세요.(숫자 1 ~ 10):"); | |
int amount = sc.nextInt(); | |
// 당첨 번호 생성 | |
ArrayList<ArrayList<Integer>> listOfLotto = new ArrayList<>(); | |
Random random = new Random(); | |
for (int i = 0; i < amount; i++) { | |
System.out.printf("%s\t", (char) ('A' + i)); | |
ArrayList<Integer> randomList = makeLotto(random); | |
listOfLotto.add(randomList); | |
printLotto(randomList); | |
} | |
System.out.println(); | |
// 로또 발표 | |
System.out.println("[로또 발표]"); | |
ArrayList<Integer> announceList = makeLotto(random); | |
System.out.print(" \t"); | |
printLotto(announceList); | |
// 로또 결과 | |
System.out.println(); | |
System.out.println("[내 로또 결과]"); | |
for (int i = 0; i < amount; i++) { | |
int resultCnt = 0; | |
ArrayList<Integer> lotto = listOfLotto.get(i); | |
// 발표된 로또에 존재하는지 확인 | |
HashMap<Integer, Boolean> numExists = new HashMap<>(); | |
for (Integer num : lotto) { | |
numExists.put(num, false); | |
} | |
for (Integer announce : announceList) { | |
if (numExists.containsKey(announce)) { | |
resultCnt++; | |
} | |
} | |
// 출력 | |
System.out.printf("%s\t", (char) ('A' + i)); | |
for (int j = 0; j < lotto.size(); j++) { | |
System.out.print(lotto.get(j)); | |
if (j != lotto.size() - 1) System.out.print(","); | |
} | |
System.out.printf(" => %d개 일치\n", resultCnt); | |
} | |
} | |
private static void printLotto(ArrayList<Integer> randomList) { | |
for (int j = 0; j < randomList.size(); j++) { | |
System.out.print(randomList.get(j)); | |
if (j != randomList.size() - 1) System.out.print(","); | |
} | |
System.out.println(); | |
} | |
private static ArrayList<Integer> makeLotto(Random random) { | |
HashSet<Integer> lotto = new HashSet<>(); | |
while (lotto.size() <= 6) { | |
if (lotto.size() == 6) break; | |
lotto.add(random.nextInt(45) + 1); | |
} | |
ArrayList<Integer> randomList = new ArrayList<>(lotto); | |
Collections.sort(randomList); | |
return randomList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment