class lotto1 {
private final static int[] lottoResult = new int[6];
/**
* 이번주 로또 1등번호
*/
public static int[] lottoResult() {
for (int i = 0; i < lottoResult.length; i++) {
lottoResult[i] = (int) ((Math.random() * 45) + 1);
// 동일 번호 확인
for (int j = 0; j < i; j++) {
if (lottoResult[i] == lottoResult[j]) {
i--;
}
}
}
// 번호 정렬
numSort(lottoResult, 0);
for (int i = 0; i < lottoResult.length; i++) {
System.out.println("이번주 1등 번호 = " + lottoResult[i]);
}
return lottoResult;
}
private static int[] purchaseNum = new int[6];
/**
* 구매한 로또 번호
*/
public static int[] purchaseLotto() {
for (int i = 0; i < purchaseNum.length; i++) {
purchaseNum[i] = (int) ((Math.random() * 45) + 1);
for (int j = 0; j < i; j++) {
if (purchaseNum[i] == purchaseNum[j]) {
i--;
}
}
}
// 일반 오름차순 정렬
Arrays.sort(purchaseNum);
// 직접 구현한 버블 소트 번호 정렬 - 성능상 젤 구림
numSort(purchaseNum, 0);
for (int i = 0; i < purchaseNum.length; i++) {
System.out.println("구매한 로또 번호 = " + purchaseNum[i]);
}
return purchaseNum;
}
/**
* 이번 로또 몇개 사면 됬을까?
*/
public static void resultCheck() {
int[] oriNum = lottoResult;
Integer cnt = 0;
while (true) {
Integer cn = cnt++;
int[] purNum = purchaseLotto();
boolean equals = Arrays.equals(oriNum, purNum);
System.out.println(cn);
if (equals == true) {
break;
}
}
}
/**
* 아래 두 메소드는 1차원 정렬 메소드
* 버블 소트 정렬 성능상 젤 구림
*/
public static void numSort(int[] arr, int start) {
if (start < arr.length - 1) {
int min_index = start;
for (int i = start; i < arr.length; i++) {
if (arr[i] < arr[min_index]) {
min_index = i;
}
}
swap(arr, start, min_index);
numSort(arr, start + 1);
}
}
public static void swap(int[] arr, int index1, int index2) {
int tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
}
'Programming > JAVA' 카테고리의 다른 글
자바의 특징 (0) | 2022.10.27 |
---|---|
객체 지향 언어란? (Object Oriented Language) (0) | 2022.10.27 |
Java 컬렉션(Collection) 정리 (0) | 2022.10.27 |
[동시성 이슈해결] HashMap보다는 ConcurrentHashMap을 쓰자! (0) | 2022.10.27 |
자바 환경변수 설정(JAVA_HOME, PATH, CLASSPATH) (0) | 2022.10.26 |
댓글