본문 바로가기
2015 IT 웹 기반 개발자과정/JAVA

JAVA Lotto [로또 Ver 3.0 배열이용]

by 한여름밤의코딩 2015. 11. 3.
package Lotto;

import java.util.Random;
import java_week1.For1;

public class Lotto {
	static int[] nums = new int[45];  // 빈 배열 45개 만들기
	
	public static boolean check(int r){		
		for (int i = 0; i < nums.length; i++) {  // 빈 배열에 랜덤 수를 대입하고 1로 체크
			if(nums[r]==0){  
				nums[r]=1;
				return true;
			}
		}			
		return false;
	}
	
	public static void main(String[] args) {
			
		Random r = new Random();
		int c,cnt;
		cnt=0;
		
		do{
			c= r.nextInt(45);
			if(check(c)==true) cnt++; // 빈 배열에 랜덤수 대입 후 카운트 
			
		}while(cnt<6);
		
		for (int i = 0; i < nums.length; i++) { //배열 출력
			if(nums[i]==1) System.out.print(i+1+" ");
		}			
	}	
}