Java기초
[Java]Random 클래스 사용 로또생성기활용
jackypark
2022. 7. 11. 14:51
로또 프로그램
1부터 45까지 난수 발생
그 숫자 중 6개 맞춤 똑같은 난수가 발생할 수 있으니깐 똑같은 숫자는 다시 뽑음
1등 숫자 6개 그대로
2등 보너스 번호 맞추면 됨 6개인데 보너스 번호 포함
3등 5개
4등 4개
5등 3개
import java.util.*;
public class RandomEx {
Set<Integer> lottoNum=new LinkedHashSet<Integer>();// 로또번호 저장할곳
Set<Integer> myLottoNum=new LinkedHashSet<Integer>();//내 로또번호 저장할곳
private int cnt=0;
private int bonusNum=0;
public static void main(String[] args) {
RandomEx re=new RandomEx();
Scanner sc=new Scanner(System.in);
re.lottoMaker();//로또 번호 생성
for(;;) {
System.out.println("1.내 등수 확인하기 2. 1등 당첨번호 3. 2등 당첨번호 4. 3등 당첨번호 5. 4등 당첨번호 6. 5등 당첨번호 0.시스템종료");
int choice=sc.nextInt();
switch(choice){
case 1: re.findPlace(sc);break; // 내로또 등수 확인하기
case 2:re.firstPlace();break; //1등 당첨번호 확인
case 3:re.secondPlace();break;//2등 당첨번호 확인
case 4:re.thirdPlace();break; //3등 당첨번호 확인
case 5:re.fourthPlace();break;// 4등 당첨번호 확인
case 6:re.fifthPlace();break; //5등 당첨번호 확인
case 0:System.exit(1);//종료
default: System.out.println("잘못입력하셧습니다");break;
}
}
}
public void lottoMaker() { //로또 번호 생성기
Random ran=new Random();
while(true) {
int num=ran.nextInt(45)+1;
if(lottoNum.contains(num)) {
continue;
}else {
if(cnt<6) {// 정규번호
lottoNum.add(num);
cnt++;
}else if(cnt==6) {// 마지막 보너스 번호
lottoNum.add(num);
bonusNum=num; //보너스번호
break;
}
}
}
System.out.println("로또번호: "+lottoNum.toString());
System.out.println("보너스번호: "+bonusNum);
}
public void firstPlace() {
System.out.println("---------------1등 당첨번호----------------");
Iterator<Integer> iterator=lottoNum.iterator();
int count=0;
while(iterator.hasNext()) {
if(count==6) {
break;
}
Integer lNum=iterator.next();
System.out.print(lNum+"\t");
count++;
}
System.out.println();
}
public void secondPlace() {
System.out.println("---------------2등 당첨번호----------------");
for(int i=0;i<cnt;i++) {
Iterator<Integer> iterator=lottoNum.iterator();
int count=0;
while(iterator.hasNext()) {
if(count==i) {
System.out.print(bonusNum+"\t"); //보너스 넘출력
iterator.next();// 다음으로 넘어가
}else {
if(count==6) {//마지막 수 필요없다
break;
}else {
System.out.print(iterator.next()+"\t");
}
}
count++;
}
System.out.println();
}
}
public void thirdPlace() {
System.out.println("---------------3등 당첨번호----------------");
for(int i=0;i<cnt;i++) {
Iterator<Integer> iterator=lottoNum.iterator();
int count=0;
while(iterator.hasNext()) {
if(count==i) {
System.out.print("X\t"); //이자리에 X넣기
iterator.next();// 다음으로 넘어가
}else {
if(count==6) {//마지막 수 필요없다
break;
}else {
System.out.print(iterator.next()+"\t");
}
}
count++;
}
System.out.println();
}
}
public void fourthPlace() {
System.out.println("---------------4등 당첨번호----------------");
for(int i=0;i<cnt;i++) {
for(int j=i+1;j<cnt;j++) {
Iterator<Integer> iterator=lottoNum.iterator();
int count=0;
while(iterator.hasNext()) {
if(count==i) {
System.out.print("X\t"); //x 넣기
iterator.next();// 다음으로 넘어가
}else if(count==j) {
System.out.print("X\t"); //x넣기
iterator.next();// 다음으로 넘어가
}else {
if(count==6) {//마지막 수 필요없다
break;
}else {
System.out.print(iterator.next()+"\t");
}
}
count++;
}
System.out.println();
}
}
}
public void fifthPlace() {
System.out.println("---------------5등 당첨번호----------------");
for(int i=0;i<cnt;i++) {
for(int j=i+1;j<cnt;j++) {
for(int k=j+1;k<cnt;k++) {
Iterator<Integer> iterator=lottoNum.iterator();
int count=0;
while(iterator.hasNext()) {
if(count==i) {
System.out.print("X\t");
iterator.next();// 다음으로 넘어가
}else if(count==j) {
System.out.print("X\t");
iterator.next();// 다음으로 넘어가
}else if(count==k) {
System.out.print("X\t");
iterator.next();// 다음으로 넘어가
}else {
if(count==6) {//마지막 수 필요없다
break;
}else {
System.out.print(iterator.next()+"\t");
}
}
count++;
}
System.out.println();
}
}
}
}
public void findPlace(Scanner sc) {//내로또 등수 확인
int num=0;
boolean checkBNum=false;
int counter=0;
for(int i=0;i<6;i++) {
System.out.print("로또번호 입력:");
num=sc.nextInt();
if(myLottoNum.contains(num)) {
System.out.println("중복된값 입력 다시입력바람:");
i--; // 중복되서 i번째 입력값에 다시입력해야함으로
continue;
}else {
myLottoNum.add(num);
}
}
System.out.print("MY 로또번호: ");
for(Integer n:myLottoNum) {
System.out.print(n+"\t");
}
System.out.println();
Iterator<Integer> myIterator=myLottoNum.iterator();
while(myIterator.hasNext()) {
Integer myNum=myIterator.next();
if(lottoNum.contains(myNum)) {
if(myNum==bonusNum) {
checkBNum=true;
}else {
counter++;
}
}
}
switch(counter) {
case 6 : System.out.println("1등");break;
case 5: if(checkBNum==true) {
System.out.println("2등");
}else {
System.out.println("3등");
}
break;
case 4: System.out.println("4등");break;
case 3:System.out.println("5등");break;
default:System.out.println("꽝");break;
}
}
}