max를 45로 바꾸기
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package day_08;
import java.util.Random;
import java.util.Arrays;
public class day07_homework01 {
public static final int SIZE= 6;
public static final int MAX= 6; //45개중 6개를 뽑는다.
// 상수를 써두면 프로그램이 끝날때까지 바뀌지 않음으로 에러가 나지 않는다. 아주굿.
public static final Random random= new Random();
public static void main(String[] args) {
// 중복 없는, 로또번호 생성기
int[] lotto= new int[SIZE];
// <<의사코드>>
// lotto[i]에 랜덤값 넣기
// lotto[j]의 값이 lotto[i] 값과 같은지 비교한다
for(int i=0; i<SIZE; i++) {
lotto[i]= 1+random.nextInt(MAX);
for(int j=0; j<i; j++) {
if(lotto[j] == lotto[i]) { //비교
lotto[i] = 1 + random.nextInt(MAX); // 새로 뽑음
j= -1; //++을 했을때 다시 0이 되도록 만들어줘야한다 > 새로 뽑고 0부터 다시 돌아간다.
}
}
}
Arrays.sort(lotto); //정렬
System.out.println(Arrays.toString(lotto));
}
}
|
cs |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package day_08;
import java.util.Random;
import java.util.Arrays;
public class day07_homework02 {
public static final int SIZE;
public static final int MAX;
public static final int[] Lotto;
public static final Random random;
//45개중 6개를 뽑는다.
// 상수를 써두면 프로그램이 끝날때까지 바뀌지 않음으로 에러가 나지 않는다. 아주굿.
static {
SIZE= 6;
MAX= 6;
random= new Random();
Lotto = new int[SIZE];
}
public static void main(String[] args) {
int cnt= 0;
while(cnt<6) {
int num= 1+ random.nextInt(MAX); //로또생성, 새로 뽑음
boolean flag= true;
for(int i=0; i<cnt; i++){ // 뽑아서 하나씩 비교
if(num == Lotto[i]) { // 0부터 cnt 전까지 비교
flag= false;
break; // 나가면 아래의 if cnt++를 하지 않았으므로 while문으로 돌아가서 다시 뽑는다
}
}
if(flag) {
Lotto[cnt++] =num; //
}
}
Arrays.sort(Lotto);
System.out.println(Arrays.toString(Lotto));
}
}
|
cs |