Skip to content

Instantly share code, notes, and snippets.

@zhyunk
Created March 6, 2023 16:21
Show Gist options
  • Save zhyunk/683149711641ef9e660c4586d00f8374 to your computer and use it in GitHub Desktop.
Save zhyunk/683149711641ef9e660c4586d00f8374 to your computer and use it in GitHub Desktop.
과제 7 피드백 반영!
import java.util.Random;
import java.util.Scanner;
/*
* [과제 7. 로또 당첨 프로그램] 의 피드백 반영!
*
* 작성자 : 김지현
* 작성일 : 2023-03-06
* */
public class MiniAssignment07InFeedBack {
public static void main(String[] args) {
MyScanner my;
int cnt;
char chNo;
int[][] arrAllMyLotto;
int[] arrDangChumNumbers;
System.out.println("[로또 당첨 프로그램]");
System.out.println();
// 1. 로또 구매 개수 입력
my = new MyScanner();
cnt = my.getScannerReturnIntRange("로또 개수를 입력해주세요.(숫자 1 ~ 10):", 1, 10);
my.close();
// 1-2. 로또 구매 개수에 의한 내로또 배열 크기 지정
arrAllMyLotto = new int[cnt][45];
// 2. 입력한 개수만큼의 로또 개수 생성 후 출력
while (--cnt >= 0) {
arrAllMyLotto[cnt] = getMakeLottoNumbers();
}
chNo = 'A';
for (int[] arr: arrAllMyLotto) {
System.out.print(String.format("%c\t", chNo++));
printLottoNumbers(arr);
System.out.println();
}
// 3. 로또 당첨번호 생성
arrDangChumNumbers = getMakeLottoNumbers();
// 3. 로또 당첨번호 출력
System.out.println("\n[로또 발표]");
System.out.print("\t");
printLottoNumbers(arrDangChumNumbers);
// 4. 당첨번호와 구매 로또 비교하여 숫자 일치여부 판단
chNo = 'A';
System.out.println("\n\n[내 로또 결과]");
int sameNumCnt = 0;
for (int[] arr: arrAllMyLotto) {
System.out.print(String.format("%c\t", chNo++));
printLottoNumbers(arr);
// 당첨 숫자 확인
for (int j = 0; j < arr.length; j++) {
if (arrDangChumNumbers[j] == 1 && arrDangChumNumbers[j] == arr[j]) {
sameNumCnt++;
break;
}
}
System.out.println(String.format(" => %d개 일치", sameNumCnt));
sameNumCnt = 0;
}
}
public static void printLottoNumbers(int[] arr) {
int comma5Cnt = 5;
for (int j = 0; j < arr.length; j++) {
if (arr[j] > 0) {
System.out.print(String.format("%02d", (j + 1)));
if (--comma5Cnt >= 0) {
System.out.print(",");
}
}
}
}
public static int[] getMakeLottoNumbers() {
int[] arrLotto = new int[45];
Random rd = new Random();
int temp;
for (int i = 0; i < 6; i++) {
temp = rd.nextInt(45);
if (arrLotto[temp] == 0) {
arrLotto[temp]++;
} else {
i--;
}
}
return arrLotto;
}
static class MyScanner {
String str;
Scanner scan;
MyScanner() {
scan = new Scanner(System.in);
}
public int getScannerReturnIntRange(String text, int min, int max) {
// min = 최소(포함) 범위 , max = 최대(포함) 범위
while (true) {
System.out.print(text);
str = scan.next();
if (str.replaceAll("[0-9]", "").isEmpty()) {
if (Integer.parseInt(str) < max + 1 && Integer.parseInt(str) > min - 1)
break;
}
}
return Integer.parseInt(str);
}
public void close() {
scan.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment