최신글
hyeonga_code
Java_로또 기능 작성하기 본문
반응형
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
import java.util.*;
class Lotto {
int[] my;
int[] com;
Lotto() {
com = new int[6];
my = new int[6];
setCom();
// 생성자 호출되며 한 번 실행됨
}
void setCom() {
// 컴퓨터 숫자
for (int i = 0; i < 6; i++) {
com[i] = (int)(Math.random() * 45) + 1;
for (int j = 0; j < i; ++j) {
if (com[i] == com[j]) {
// 같은 값이 있다면
--i;
// i 하나 감소
break;
// 다시 i값 진행
}
}
}
}
void self() {
Scanner in = new Scanner(System.in);
for (int i = 0; i < 6; i++) {
do {
System.out.print(i + 1 + "st number: ");
my[i] = in.nextInt();
} while (my[i] < 1 || my[i]>45);
for (int j = 0; j < i; ++j) {
if (my[i] == my[j]) {
--i;
break;
}
}
}
}
void auto() {
// 컴퓨터 숫자 지정과 동일
for (int i = 0; i < 6; i++) {
my[i] = (int)(Math.random() * 45) + 1;
for (int j = 0; j < i; ++j) {
if (my[i] == my[j]) {
// 같은 값이 있다면
--i;
// i 하나 감소
break;
// 다시 i값 진행
}
}
}
}
int getCount() {
// 맞춘 개수
int count = 0;
// 개수 확인
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
if (com[i] == my[j]) {
++count;
break;
}
}
}
return count;
}
void view() {
getCount();
System.out.print("Com's number : ");
for (int i = 0; i < com.length - 1; ++i) {
System.out.print(com[i] + " , ");
}
System.out.println(com[5]);
System.out.print("입력하신 수 : ");
for (int i = 0; i < 5; ++i) {
System.out.print(my[i] + " , ");
}
System.out.println(my[5]);
}
void reset() {
}
}
public class same {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Lotto lotto = new Lotto();
while (true) {
in = new Scanner(System.in);
System.out.println("==========");
System.out.println(" - menu - ");
System.out.println("1. Self");
System.out.println("2. Auto");
System.out.println("3. Reset");
System.out.println("4. End");
System.out.println("==========");
System.out.print("Choose menu: ");
int menu = in.nextInt();
switch (menu) {
case 1:
lotto.self();
System.out.println("Match : " + lotto.getCount() + ".");
case 2:
lotto.auto();
System.out.println("Match : " + lotto.getCount() + ".");
case 3:
lotto.view();
lotto.setCom();
break;
case 4:
lotto.view();
System.out.println("End Program.");
System.exit(0);
default:
System.out.println("Wrong!");
}
}
}
}
|
반응형
'Java' 카테고리의 다른 글
Java의 신_Chapter 02_Hello God of Java (0) | 2024.03.27 |
---|---|
Java의 신_Chapter 01_Programming (0) | 2024.03.27 |
Java_룸 예약 시스템 작성하기 (0) | 2023.10.31 |
Java_성적 관리 프로그램 작성하기 (0) | 2023.10.30 |
Java_소수 구하는 코드 작성하기 (0) | 2023.10.30 |