반응형
import java.util.Random;
import java.util.Scanner;
public class Lotto {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int pur_num = 0; // 구매 횟수
static int cnt = 0; // 횟수 카운트
int[] arr; // arr 배열
Lotto() { // Lotto 생성자 생성
arr = new int[6];
}
public void input() {
System.out.print("구매 횟수를 입력하세요 : ");
pur_num = sc.nextInt();
System.out.println();
}
public void calc() {
boolean chk;
for(int i = 0; i < 6;) {
arr[i] = random.nextInt(45) + 1; // 1 ~ 45 개의 랜덤 정수 생성
chk = false;
for(int j = 0; j < i; j++) { // i,j 같은값 여부 확인
if(arr[i] == arr[j]) {
chk = true;
break;
}
}
if(!chk) i++;
}
for(int x = 0; x < 5; x++) { // 우선순위 연산
for(int y = x+1; y < 6; y++) {
if(arr[x] > arr[y]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}
public void output() {
for(int i=0; i<arr.length; i++) {
System.out.printf("%2d ", arr[i]);
}
System.out.println();
}
public void print() {
input();
for(int i=0; i<pur_num; i++) {
calc();
output();
}
}
}
public class Test {
public static void main(String[] args) {
Lotto lotto = new Lotto();
lotto.print();
}
}
유의할점!
random 함수
- 정수 1 ~ 45개 생성시 ( random.nextInt(45) + 1 )
boolean 정의
- 변수 boolean 을 정의함으로써 배열내에 같은 값 여부확인 코드
- 같은 값 여부 확인
선택정렬
- temp 를 사용하여 배열값을 바꿔주는 선택정렬
반응형