import java.util.Arrays;
import java.util.Random;
public class Day07Lottery {
static Random rand = new Random();
public static void main(String[] args) {
// 로또 번호 생성기
int[] lotto = new int[6];
int cnt = 0;
while (cnt < 6) {
lotto[cnt] = 1 + rand.nextInt(45);
for (int i = 0; i < cnt; i++) {
if (lotto[i] == lotto[cnt]) { // lotto[0]부터 lotto[cnt]까지 비교
cnt--; // 값이 중복이면, 값을 새로 넣고, 다시 비교해봐야 함. 해당 lotto[cnt]반복 위해 cnt--.
} // if
} // for
cnt++;
} // while
System.out.println(Arrays.toString(lotto));
}
public static void test02(String[] args) {
// 로또 번호 생성기-boolean 이용
int[] lotto = new int[6];
int cnt = 0;
boolean overlap = false; // 중복 여부 검사
while (cnt < 6) {
lotto[cnt] = 1 + rand.nextInt(45); // 1~45 중 랜덤한 6개의 숫자를 배열 lotto에 넣는다.
for (int i = 0; i < cnt; i++) {
overlap = lotto[i] == lotto[cnt]; // overlap이 true : 중복이 true면 다음 if문 실행.
if (overlap) {
cnt--;
} // if
} // for
cnt++;
} // while
System.out.println(Arrays.toString(lotto));
} //test02
public static void test03(String[] args) {
int[] lotto = new int[6];
boolean overlap = false;
for(int i =0; i<6; i++){
lotto[i] = 1+rand.nextInt(45);
for(int j=0; j<i; j++){
overlap = lotto[i]==lotto[j];
if(overlap){
i--;
}
} // for-j
} // for-i
System.out.println(Arrays.toString(lotto));
} // test03
}
======================================================
<추가 예제>
import java.util.Arrays;
import java.util.Random;
public class Day08Homework_lottery {
static Random rand = new Random();
static final int MAX = 45;
public static void test01(String[] args) {
// TODO Auto-generated method stub
int[] lotto = new int[6];
int cnt =0;
aaa: while (cnt < 6) {
lotto[cnt] = 1 + rand.nextInt(45);
for(int i=0; i<cnt; i++){
if(lotto[cnt]==lotto[i]){
continue aaa;
}
}
cnt++;
} // while
System.out.println(Arrays.toString(lotto));
} // test01
public static void test02(String[] args) {
int[] lotto = new int[6];
int cnt = 0;
while (cnt < 6) {
lotto[cnt] = 1 + rand.nextInt(45);
for(int i=0; i<cnt; i++){
if(lotto[cnt]==lotto[i]){
lotto[cnt] = 1 + rand.nextInt(45);
i = -1;
}
}
cnt++;
} // while
System.out.println(Arrays.toString(lotto));
} //test02
public static void test03(String[] args) {
int[] lotto = new int[6];
int cnt = 0;
while (cnt < 6) {
lotto[cnt] = 1 + rand.nextInt(45);
for(int i=0; i<cnt; i++){
if(lotto[cnt]==lotto[i]){
cnt--;
break;
}
}
cnt++;
} // while
System.out.println(Arrays.toString(lotto));
} //test03
public static void test04(String[] args) {
int[] lotto = new int[6];
int cnt = 0;
while (cnt < 6) {
int number = 1 + rand.nextInt(45);
boolean flag =true;
for(int i=0; i<cnt; i++){
if(number==lotto[i]){
flag = false;
break;
}
}
if(flag){
lotto[cnt] = number;
cnt++;
}
} // while
System.out.println(Arrays.toString(lotto));
} //test04
public static void test05(String[] args) {
int[] balls = new int[MAX];
int[] lotto = new int[6];
int cnt = 0;
for(int i =0; i<MAX; i++){
balls[i] = i+1; // balls에 1~45 넣기
}
while(cnt<lotto.length){
int idx = rand.nextInt(MAX); // 0~44
if(balls[idx] !=0){
lotto[cnt] = balls[idx];
balls[idx] = 0;
cnt++;
}
}
System.out.println(Arrays.toString(lotto));
} //test05
public static void test06(String[] args) {
boolean[] flags = new boolean[MAX]; // 초기화 안 해도 기본값 false
int[] lotto = new int[6];
int cnt = 0;
while(cnt<lotto.length){
int idx = rand.nextInt(MAX);
if(flags[idx] != true){
lotto[cnt] = idx+1;
flags[idx] = true;
cnt++;
}
} // while
System.out.println(Arrays.toString(lotto));
} //test06
public static void main(String[] args) {
int[] balls = new int[MAX];
int cnt =0;
while(cnt<6){
int idx = rand.nextInt(MAX);
if(balls[idx]==0){
balls[idx] = idx+1;
cnt++;
} // if
} // while
for(int i=0; i<MAX; i++){
if(balls[i]!=0){
System.out.print(balls[i] + " ");
}
} // for
} // test07 - 순서대로 정렬도 된다.
}
댓글 없음:
댓글 쓰기