From b6b8cffa516a68d10c63bd95faa6d1a4dd1f9233 Mon Sep 17 00:00:00 2001 From: gilhyeonwoo Date: Thu, 13 Feb 2025 19:58:21 +0900 Subject: [PATCH 1/3] =?UTF-8?q?1=EB=8B=A8=EA=B3=84=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=EA=B5=AC=EB=A7=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Lotto.java | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/Lotto.java diff --git a/src/main/java/Lotto.java b/src/main/java/Lotto.java new file mode 100644 index 00000000..e962b282 --- /dev/null +++ b/src/main/java/Lotto.java @@ -0,0 +1,44 @@ +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.nio.charset.StandardCharsets; + +public class Lotto { + + private final Set lottoNumbers = new HashSet<>(); + private List sortedLotto = new ArrayList<>(); + + public Lotto() { + setLottoNumbers(); + SetToList(); + } + + public void setLottoNumbers() { + Random random = new Random(); + while (lottoNumbers.size() < 6){ + int number = random.nextInt(45) + 1; + lottoNumbers.add(number); + } + } + public List SetToList() { + sortedLotto = new ArrayList<>(lottoNumbers); + Collections.sort(sortedLotto); + return sortedLotto; + } + + public static void main(String[] args) { + System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8)); + + Scanner scanner = new Scanner(System.in); + System.out.println("구입금액을 입력해주세요:"); + + int money = scanner.nextInt(); + int price = 1000; + int loop = money/price; + System.out.println(loop + "개를 구매했습니다."); + for(int i = 0; i < loop; i++) { + Lotto lotto = new Lotto(); + System.out.println(lotto.sortedLotto); + } + } +} From 96f43de96e46293e7a24c379b9f0e64c6ef99161 Mon Sep 17 00:00:00 2001 From: gilhyeonwoo Date: Sat, 15 Feb 2025 15:52:18 +0900 Subject: [PATCH 2/3] =?UTF-8?q?2=EB=8B=A8=EA=B3=84=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=8B=B9=EC=B2=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Lotto.java | 82 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/src/main/java/Lotto.java b/src/main/java/Lotto.java index e962b282..13582d7e 100644 --- a/src/main/java/Lotto.java +++ b/src/main/java/Lotto.java @@ -1,12 +1,19 @@ import java.io.PrintStream; import java.nio.charset.StandardCharsets; +import java.sql.SQLOutput; import java.util.*; import java.nio.charset.StandardCharsets; + + public class Lotto { private final Set lottoNumbers = new HashSet<>(); private List sortedLotto = new ArrayList<>(); + private final List> lottoNum = new ArrayList<>(); + + int[] rank = new int[7]; + int[] winningPrize = {0, 0, 0, 5000, 50000, 1500000, 2000000000}; public Lotto() { setLottoNumbers(); @@ -25,20 +32,81 @@ public List SetToList() { Collections.sort(sortedLotto); return sortedLotto; } + public void printLotto(int inputPrice) { + int price = 1000; + int loop = inputPrice/price; + for(int i = 0; i < loop; i++) { + Lotto generateLotto = new Lotto(); + System.out.println(generateLotto.sortedLotto); + lottoNum.add(generateLotto.sortedLotto); + } + //lottoNum 이 생성된 로또 번호들을 저장 하고 있음 + } + public List StringToInt(String[] StringlastNum) { + List WinningNum = new ArrayList<>(); + for (String value : StringlastNum) { + int lastNumber = Integer.parseInt(value); + WinningNum.add(lastNumber); + } + return WinningNum; + } + + public void checkingWinningnumbers(List lastWinningNum) { + + for (List singleLotto : lottoNum) { + int matchCount = countMatchingnumbers(singleLotto, lastWinningNum); + rank[matchCount]++; + } + for (int i = 3; i<7; i++) { + System.out.println(i + "개 일치" + "(" + winningPrize[i] + "원)- " + rank[i] +"개"); + } + } + + public int countMatchingnumbers(List singleLotto, List lastWinningNum) { + int count = 0; + for (int value : lastWinningNum) { + count += isMaching(value, singleLotto); + } + return count; + } + + public int isMaching(int value, List singleLotto) { + if (singleLotto.contains(value)){ + return 1; + } + return 0; + } + //TODO: 수익률을 계산하는 매서드 추가 + public double getProfit(int money) { + double profit = 0; + double prize = 0; + for (int i = 3; i<7; i++){ + if(rank[i] > 0){ + prize += winningPrize[i] * rank[i]; + } + } + profit = prize / money; + return profit; + } + public static void main(String[] args) { System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8)); + Lotto lotto = new Lotto(); Scanner scanner = new Scanner(System.in); System.out.println("구입금액을 입력해주세요:"); int money = scanner.nextInt(); - int price = 1000; - int loop = money/price; - System.out.println(loop + "개를 구매했습니다."); - for(int i = 0; i < loop; i++) { - Lotto lotto = new Lotto(); - System.out.println(lotto.sortedLotto); - } + lotto.printLotto(money); + + scanner.nextLine(); + + System.out.println("지난 주 당첨 번호를 입력해 주세요."); + String inputWinningNum = scanner.nextLine(); + String[] WinningNum = inputWinningNum.split(","); + List intWinningNum = lotto.StringToInt(WinningNum); + lotto.checkingWinningnumbers(intWinningNum); + System.out.println("총 수익률은 " + lotto.getProfit(money) + "입니다."); } } From 2353086d1222bd4ffd37142af45aaca256e9ea55 Mon Sep 17 00:00:00 2001 From: gilhyeonwoo Date: Sat, 15 Feb 2025 17:37:44 +0900 Subject: [PATCH 3/3] =?UTF-8?q?3=EB=8B=A8=EA=B3=84=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=8B=B9=EC=B2=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit indent depth 2이하 조건 맞추면서 하기 너무 어려워요.. 지금 보다 더 기능을 쪼개서 코딩하면 가능할 것 같다는 생각 1등 당첨 출력에서 7개 일치를 6개 일치로 출력되는게 해야하는데.. --- src/main/java/Lotto.java | 76 +++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/src/main/java/Lotto.java b/src/main/java/Lotto.java index 13582d7e..cd457f8c 100644 --- a/src/main/java/Lotto.java +++ b/src/main/java/Lotto.java @@ -1,10 +1,6 @@ import java.io.PrintStream; import java.nio.charset.StandardCharsets; -import java.sql.SQLOutput; import java.util.*; -import java.nio.charset.StandardCharsets; - - public class Lotto { @@ -12,76 +8,98 @@ public class Lotto { private List sortedLotto = new ArrayList<>(); private final List> lottoNum = new ArrayList<>(); - int[] rank = new int[7]; - int[] winningPrize = {0, 0, 0, 5000, 50000, 1500000, 2000000000}; + private Integer[] rank = new Integer[8]; // 당첨 개수 저장 + private final Integer[] winningPrize = {0, 0, 0, 5000, 50000, 1500000, 30000000, 2000000000}; + private int bonusBall = 0; public Lotto() { setLottoNumbers(); SetToList(); + Arrays.fill(rank, 0); // rank 배열을 0으로 초기화 } public void setLottoNumbers() { Random random = new Random(); - while (lottoNumbers.size() < 6){ + while (lottoNumbers.size() < 6) { int number = random.nextInt(45) + 1; lottoNumbers.add(number); } } + public List SetToList() { sortedLotto = new ArrayList<>(lottoNumbers); Collections.sort(sortedLotto); return sortedLotto; } + public void printLotto(int inputPrice) { int price = 1000; - int loop = inputPrice/price; - for(int i = 0; i < loop; i++) { + int loop = inputPrice / price; + for (int i = 0; i < loop; i++) { Lotto generateLotto = new Lotto(); System.out.println(generateLotto.sortedLotto); lottoNum.add(generateLotto.sortedLotto); } - //lottoNum 이 생성된 로또 번호들을 저장 하고 있음 } + public List StringToInt(String[] StringlastNum) { List WinningNum = new ArrayList<>(); for (String value : StringlastNum) { - int lastNumber = Integer.parseInt(value); + int lastNumber = Integer.parseInt(value.trim()); // 공백 제거 후 변환 WinningNum.add(lastNumber); } return WinningNum; } - public void checkingWinningnumbers(List lastWinningNum) { + public int PureStringToInt(StringBuilder str) { + return Integer.parseInt(str.toString().trim()); // 공백 제거 후 변환 + } + public void checkingWinningnumbers(List lastWinningNum) { for (List singleLotto : lottoNum) { int matchCount = countMatchingnumbers(singleLotto, lastWinningNum); - rank[matchCount]++; + boolean hasBonus = isBonusBall(singleLotto); + + if (matchCount == 5 && hasBonus) { + rank[6]++; // 5개 + 보너스 볼 = 2등 + } else if (matchCount <= 6) { + rank[matchCount]++; // 일반 당첨 처리 + } } - for (int i = 3; i<7; i++) { - System.out.println(i + "개 일치" + "(" + winningPrize[i] + "원)- " + rank[i] +"개"); + + for (int i = 3; i < 8; i++) { + if (i == 6) { + System.out.println("5개 일치, 보너스 볼 일치 (" + winningPrize[i] + "원)- " + rank[i] + "개"); + } else { + System.out.println(i + "개 일치 (" + winningPrize[i] + "원)- " + rank[i] + "개"); + } } } public int countMatchingnumbers(List singleLotto, List lastWinningNum) { int count = 0; for (int value : lastWinningNum) { - count += isMaching(value, singleLotto); + if (value != bonusBall) { // 보너스 볼을 제외하고 비교 + count += isMatching(value, singleLotto); + } } return count; } - public int isMaching(int value, List singleLotto) { - if (singleLotto.contains(value)){ + public int isMatching(int value, List singleLotto) { + if (singleLotto.contains(value)) { return 1; } return 0; } - //TODO: 수익률을 계산하는 매서드 추가 + public double getProfit(int money) { + if (money == 0) return 0; // 0으로 나누기 방지 + double profit = 0; double prize = 0; - for (int i = 3; i<7; i++){ - if(rank[i] > 0){ + for (int i = 3; i < 8; i++) { + if (rank[i] > 0) { prize += winningPrize[i] * rank[i]; } } @@ -89,6 +107,13 @@ public double getProfit(int money) { return profit; } + public void getbonusBall(StringBuilder bonus) { + bonusBall = PureStringToInt(bonus); + } + + public boolean isBonusBall(List singleLotto) { + return singleLotto.contains(bonusBall); // 보너스 볼이 포함되었는지만 체크 + } public static void main(String[] args) { System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8)); @@ -97,14 +122,17 @@ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("구입금액을 입력해주세요:"); - int money = scanner.nextInt(); + Integer money = scanner.nextInt(); lotto.printLotto(money); scanner.nextLine(); System.out.println("지난 주 당첨 번호를 입력해 주세요."); - String inputWinningNum = scanner.nextLine(); - String[] WinningNum = inputWinningNum.split(","); + StringBuilder inputWinningNum = new StringBuilder(scanner.nextLine()); + String[] WinningNum = inputWinningNum.toString().split(","); + System.out.println("보너스 볼을 입력해 주세요."); + StringBuilder bonusBall = new StringBuilder(scanner.nextLine()); + lotto.getbonusBall(bonusBall); List intWinningNum = lotto.StringToInt(WinningNum); lotto.checkingWinningnumbers(intWinningNum); System.out.println("총 수익률은 " + lotto.getProfit(money) + "입니다.");