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]); 
} 
} 
} 
'IT > JAVA' 카테고리의 다른 글
| JAVA - 예제 - 월(Month) 구하기 (0) | 2023.06.14 | 
|---|---|
| JAVA - 실습 - 주사위 게임 (0) | 2023.06.14 | 
| JAVA - 실습 - 다마고치 키우기 (0) | 2023.06.13 | 
| JAVA - 예제 - 간단한 TV 전원 및 볼륨 조절 프로그램(리모컨) (0) | 2023.06.13 | 
| JAVA - 예제 - 생성자 (0) | 2023.06.12 | 
댓글