자바

버블 소팅 이용한 로또 예제

chantleman 2024. 7. 12. 17:54
int[] temp = new int[45];		
		
//temp 1~45 숫자 담기        
for(int i=0; i< temp.length; i++) {
	temp[i] = i+1;
}
			
		
// temp 값 섞기
for(int i=0; i<10000; i++) {
	int ran = (int)(Math.random()*45);
	int t = temp[0];
	temp[0] = temp[ran];
	temp[ran] = t;
}
		
// 6자리 로또 값에 temp 값 0~5까지의 값 복사
int[] lotto = new int[6];
System.arraycopy(temp, 0, lotto, 0, 6);
		
// 숫자 정렬 해보기
for(int i=0; i<lotto.length-1; i++) { 
	for(int j=0; j<lotto.length-1; j++) {
		if(lotto[j]> lotto[j+1]) {
			int t = lotto[j];
			lotto[j] = lotto[j+1];
			lotto[j+1] = t;
		}
	}
}
		
// 정렬 후 출력 할 것.
System.out.print("[");
for(int l : lotto) {
	System.out.print(l+", ");
}
System.out.println("]");

'자바' 카테고리의 다른 글

constructor, 생성자  (0) 2024.07.15
버블 소팅  (0) 2024.07.12
배열 복사  (0) 2024.07.12
필드  (0) 2024.07.12
배열, 필드 데이터 담기  (0) 2024.07.12