티스토리 뷰


/*
로또 프로그램
1. 로또 번호 생성 1~45 랜덤숫자 6개
2. 로또 추첨 및 생성해놓은 숫자로 확인
3. 1~2를 일정 랭크가 나올 때까지 반복 (rank)
 */

package cbnu;

import java.util.Scanner;

public class LottoGame {
static int size = 6;
static GenerateLottoNumber gln = new GenerateLottoNumber();
static Lotto lotto = new Lotto();
static int[] myNum = null;
static int[] pubNum = null;
static int[] good = null;
static int rank = 6;

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("- 로또 번호 생성 및 추첨 프로그램이다");
while (true) {
System.out.println("\n\n1. 번호 입력\n2. 번호 생성\n3. 추첨\n4. 번호자동생성&추첨 "
+ rank + "등 이상 될 때까지 반복(추첨번호도 리셋)");
Scanner scan = new Scanner(System.in);
int select = scan.nextInt();

if (select == 1) {
System.out.println("6개 번호 입력 : ");
myNum = new int[size + 1];
scan = new Scanner(System.in);
for (int i = 0; i < size; i++) {
myNum[i] = scan.nextInt();
}
} else if (select == 2) {
function2();
} else if (select == 3 && myNum != null) {
function3();
} else if (select == 4) {
while (true) {
function2();
function3();
if (lotto.getCnt() == rank)
break;
}
} else {
System.out.println("잘못된 입력");
}
}
}

private static void function2() {
// TODO Auto-generated method stub
myNum = gln.generating();
// printMessage("\n생성된 번호     : ", myNum);
}

private static void function3() {
// TODO Auto-generated method stub
lotto.exe(); //추첨 시작
pubNum = lotto.getNumber(); 

printMessage("\n내 번호     : ", myNum);
printMessage("\n추첨된 번호 : ", pubNum);
System.out.print("  Bonus : " + lotto.gln.getBonus());

good = lotto.check_num(myNum);
System.out.print("\n일치하는 번호 : ");
for (int i = 0; i < lotto.getCnt(); i++) {
System.out.print(" " + good[i]);
}
System.out.print("  [" + lotto.getCnt() + "개]");

// Rank 계산
if (lotto.getCnt() > 3) {
if (lotto.getCnt() == 6) {
System.out.println("  1등! 인생역전!");
} else if (lotto.getCnt() == 5 && lotto.bonus == true) {
System.out.println("  2등이다!");
} else if (lotto.getCnt() == 5) {
System.out.println("  3등!");
} else if (lotto.getCnt() == 4) {
System.out.println("  4등!");
} else {
System.out.println("  5등이네요");
}
} else {
System.out.println(" 꽝!!! 다음 기회에...");
}
}

private static void printMessage(String string, int[] Num) {
// TODO Auto-generated method stub
System.out.print(string);
for (int i = 0; i < Num.length - 1; i++) {
System.out.print(" " + Num[i]);
}
}
}

//로또 번호를 생성
class GenerateLottoNumber {
static int size = 7;
int number[];
int bonus;

//실제 로또 번호를 생성하는 메소드
int[] generating() {
number = new int[size];
for (int i = 0; i < size; i++) {
number[i] = (int) (Math.random() * 45) + 1;
}
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (number[i] == number[j] && i!=j) {
number[j] = (int) (Math.random() * 45) + 1;
}
}
}
bonus = number[size - 1];
selectSort();

return number;
}

public int getBonus() {
return bonus;
}

private void selectSort() {
// TODO Auto-generated method stub
for (int i = 0; i < number.length - 2; i++) {
for (int j = i + 1; j < number.length - 1; j++) {
if (number[i] > number[j])
swap(number, i, j);
}
}
}

private static void swap(int[] array, int i, int j) {
// TODO Auto-generated method stub
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

class Lotto {
GenerateLottoNumber gln;
int[] number, temp;
static int cnt, size = 7;
static boolean bonus;

public int[] getNumber() {
return number;
}

public int getCnt() {
return cnt;
}

void exe() {
bonus = false;
gln = new GenerateLottoNumber();
number = gln.generating();
}

int[] check_num(int[] myNum) {
cnt = 0;
temp = new int[size];
for (int i = 0; i < myNum.length - 1; i++) {
for (int j = 0; j < number.length; j++) {
if (myNum[i] == number[j]) {
if (j == 6) {
bonus = true;
} else
temp[cnt++] = number[j];
}
}
}
return temp;
}
}




댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함