Created
June 10, 2023 15:49
-
-
Save se-jeongit/6e490922cac4a00bc865d2b5f4bdc3d0 to your computer and use it in GitHub Desktop.
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.Random; | |
import java.util.Scanner; | |
public class Assignment7_Lotto { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("[로또 당첨 프로그램]\n"); | |
System.out.print("로또 개수를 입력해 주세요.(숫자 1 ~ 10):"); | |
int max = sc.nextInt(); | |
sc.close(); | |
ArrayList<int[]> purchasedNum = new ArrayList<int[]>(); | |
for (int i = 0; i < max; i++) { | |
int[] randomNum = new int[6]; | |
createRandomNum(randomNum); | |
System.out.print((char) ('A' + i) + "\t"); | |
arraySort(randomNum); | |
purchasedNum.add(randomNum); | |
arrayReturn(randomNum, "y"); | |
} | |
System.out.print("\n[로또 발표]\n"); | |
int[] winningNum = new int[6]; | |
createRandomNum(winningNum); | |
System.out.print("\t"); | |
arraySort(winningNum); | |
arrayReturn(winningNum, "y"); | |
System.out.print("\n[내 로또 결과]\n"); | |
for (int i = 0; i < max; i++) { | |
int check = 0; | |
int[] randomNum = new int[6]; | |
for (int j = 0; j < randomNum.length; j++) { | |
for (int k = 0; k < winningNum.length; k++) { | |
randomNum = purchasedNum.get(i); | |
if (randomNum[j] == winningNum[k]) | |
check++; | |
} | |
} | |
System.out.print((char) ('A' + i) + "\t"); | |
arrayReturn(randomNum, "n"); | |
System.out.print(" => " + check + "개 일치\n"); | |
} | |
} | |
public static void createRandomNum(int[] array) { | |
for (int i = 0; i < 6; i++) { | |
Random rd = new Random(); | |
int rdNumber = rd.nextInt(45) + 1; | |
for (int j = 0; j < 6; j++) { | |
while (rdNumber == array[j]) { | |
rdNumber = rd.nextInt(45) + 1; | |
} | |
} | |
array[i] = rdNumber; | |
} | |
} | |
public static void arraySort(int[] array) { | |
for (int i = 0; i < array.length; i++) { | |
for (int j = 0; j < array.length; j++) { | |
int temporary = array[i]; | |
if (temporary < array[j]) { | |
temporary = array[j]; | |
array[j] = array[i]; | |
array[i] = temporary; | |
} | |
} | |
} | |
} | |
public static void arrayReturn(int[] array, String answer) { | |
for (int i = 0; i < array.length; i++) { | |
if (i != array.length - 1) | |
System.out.print(String.format("%02d", array[i]) + ", "); | |
else | |
System.out.print(String.format("%02d", array[i])); | |
} | |
if (!answer.equals("n")) | |
System.out.println(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment