-
Notifications
You must be signed in to change notification settings - Fork 0
/
로또예제2
89 lines (64 loc) · 1.29 KB
/
로또예제2
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package Lotto;
import java.util.Arrays;
public class LottoNum {
private int[] lots;
private int base; //45
private int ballNum; //6
public LottoNum(int base, int ballNum) {
super();
this.ballNum = ballNum;
this.base = base;
//배열 생성
lots = new int[ballNum];
}
public LottoNum() {
this(45,6);
}
public void print() {
for (int i = 0; i < lots.length; i++) {
if (i == lots.length-1) {
System.out.printf("%d"+",", lots[i]);
} else {
System.out.printf("%d"+"," , lots[i]);
}
}
System.out.println();
}
private int rand() {
return (int)(Math.random() * base) +1 ;
}
public boolean contain(int n) {
boolean isC = false;
for(int i = 0; i <lots.length; i++) {
if(lots[i] == n) {
isC = true;
break;
}
}
return isC;
}
public void make() {
Arrays.fill(lots, 0);
int count = 0;
while(count != ballNum) {
int temp = rand();
if(! contain(temp)) {
lots[count++] = temp;
}
}
Arrays.sort(lots);
}
public int[] getLots() {
return lots;
}
}
--------------------------------------------------------------------------------
메인
package Lotto;
public class LottoNumMain {
public static void main(String[] args) {
LottoNum ln = new LottoNum(45,6);
ln.make();
ln.print();
}
}