본문 바로가기
IT/JAVA

JAVA - 실습 - 로또 번호 추출

by 비준 2023. 6. 14.

1. 문제

  -> 로또 번호 추출 프로그램 만들기
  -> 1 ~ 45까지 임의의 숫자 6개 추출
  -> 번호는 중복되면 안됨
  -> 출력 시 오름차순으로 정렬

 

2.코드

package game;

public class Study2 {
public static void main(String[] args) {
// 로또 번호 추출 프로그램 만들기
// 1 ~ 45까지 임의의 숫자 6개 추출
// 번호는 중복되면 안됨
// 출력 시 오름차순으로 정렬
int[] rotoo = new int[6];
int[] buket = new int[46];
int randdomBall = 0,j = 0,temp = 0;
for(int i = 1; i <= rotoo.length; i++) { //랜덤 번호 추출
randdomBall = (int)(Math.random() * 45) + 1;
if(buket[randdomBall] == 1) { //중복 시 다시뽑기
i--;
continue;
}else {
buket[randdomBall] = 1;
}
}

for(int i = 0; i < buket.length; i++) { //6개 숫자 담기
if(buket[i] == 1) {
rotoo[j] = i;
j++;
}
}

for(int i = 0; i < rotoo.length; i++) {
temp = rotoo[i];
for(j = 0; j < rotoo.length; j++) {
if(temp < rotoo[j]) {
temp = rotoo[j];
rotoo[j] = rotoo[i];
rotoo[i] = temp;
}
}
}
for(int i = 0; i < rotoo.length; i++) {
System.out.println(rotoo[i]);
}

}
}

댓글