Skip to content

Instantly share code, notes, and snippets.

@ramyo564
Created November 7, 2023 17:48
Show Gist options
  • Save ramyo564/c5cb44f515cf5cf84dd4e2f7ba717a69 to your computer and use it in GitHub Desktop.
Save ramyo564/c5cb44f515cf5cf84dd4e2f7ba717a69 to your computer and use it in GitHub Desktop.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
List<Set<Integer>> sets = new ArrayList<>();
Set<Integer> lotto = new HashSet<>();
System.out.println("[로또 당첨 프로그램]");
System.out.println();
System.out.print("로또 개수를 입력해 주세요.(숫자 1 ~ 10):");
int numSets = scanner.nextInt();
for (int i = 0; i < numSets; i++) {
Set<Integer> set = new HashSet<>();
while (set.size() != 6) {
int number = random.nextInt(45) + 1;
set.add(number);
}
sets.add(set);
}
// A, B, C, ... 형태로 로또 번호 세트 출력
char label = 'A';
for (Set<Integer> lottoSet : sets) {
System.out.print(label + " ");
int i = 0;
List<Integer> sortedList = new ArrayList<>(lottoSet);
Collections.sort(sortedList);
for (int number : sortedList) {
System.out.printf("%02d", number);
// 마지막 번호 뒤에는 , 출력하지 않음
if (i < 5) {
System.out.print(",");
}
i++;
}
int matchCount = countMatchingNumbers(lottoSet, lotto);
System.out.printf(" => %d개 일치", matchCount);
System.out.println();
label++;
}
System.out.println();
System.out.println("[로또 발표]");
while (lotto.size() != 6) {
int lottoNumber = random.nextInt(45) + 1;
lotto.add(lottoNumber);
}
int i = 0;
System.out.print(" ");
List<Integer> sortedLotto = new ArrayList<>(lotto);
Collections.sort(sortedLotto);
for (int number : sortedLotto) {
System.out.printf("%02d", number);
// 마지막 번호 뒤에는 , 출력하지 않음
if (i < 5) {
System.out.print(",");
}
i++;
}
System.out.println();
System.out.println();
System.out.println("[내 로또 결과]");
// A, B, C, ... 형태로 로또 번호 세트 출력
label = 'A';
for (Set<Integer> lottoSet : sets) {
System.out.print(label + " ");
i = 0;
List<Integer> sortedSet = new ArrayList<>(lottoSet);
Collections.sort(sortedSet);
for (int number : sortedSet) {
System.out.printf("%02d", number);
// 마지막 번호 뒤에는 , 출력하지 않음
if (i < 5) {
System.out.print(",");
}
i++;
}
int matchCount = countMatchingNumbers(lottoSet, lotto);
System.out.printf(" => %d개 일치", matchCount);
System.out.println();
label++;
}
}
private static int countMatchingNumbers(Set<Integer> set1, Set<Integer> set2) {
int count = 0;
for (int number : set1) {
if (set2.contains(number)) {
count++;
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment