728x90
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
|
package baseball;
import java.util.Arrays;
import java.util.Scanner;
public class Baseball {
int u_num[]=new int[3];
int r_num[]=new int[3];
boolean clear;
public int[] bRandom() {
boolean swit[] = new boolean[10];
int r, w = 0;
while(w < 3) {
r = (int)(Math.random() * 10); // 0 ~ 9
if(swit[r] == false) {
swit[r] = true;
r_num[w] = r + 1; // 1 ~ 10
w++;
}
}
System.out.println(Arrays.toString(r_num));
return r_num;
}
public void userInput() {
Scanner sc = new Scanner(System.in);
while(true) {
for(int i = 0;i < u_num.length; i++) {
System.out.print((i + 1) + "번째 수 = ");
u_num[i] = sc.nextInt();
}
if(u_num[0] == u_num[1]
|| u_num[0] == u_num[2]
|| u_num[1] == u_num[2]) {
System.out.println("같은 숫자가 있습니다. 다시 입력해 주세요");
continue;
}
break;
}
//return u_num; class 할때는 return 값 필요없다 어디든지 접근이 가능하기 때문에
}
int Ball() {// 클래스 내에서 호출이기때문에 public할 필요없
int ball = 0;
for(int i = 0;i < r_num.length; i++) {
for(int j = 0;j < r_num.length; j++) {
if(u_num[i] == r_num[j] && i != j) {
ball++;
}
}
}
return ball;
}
int Strike() {
int strike = 0;
for(int i = 0;i < r_num.length; i++) {
if(u_num[i] == r_num[i]) {
strike++;
}
}
return strike;
}
public void loop() {
int loop = 0;
while(loop < 10) {
// TODO:2. user input -> 3개
userInput();
// TODO:3. 비교
// ball
int ball = Ball();
// strike
int strike = Strike();
if(strike > 2) {
// 맞췄을 때
clear = true;
break;
}
// TODO:4. 메시지 ? strike ? ball
System.out.println(strike + "스트라이크 " + ball + "볼입니다");
loop++;
}
}
public void resultPrint() {
if(clear) {
System.out.println("Game Clear!!!");
}
else {
System.out.println("Game Over~");
}
}
|
cs |
" target="_blank" rel="noopener" data-mce-href="http://
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package main;
import baseball.Baseball;
public class MainClass {
public static void main(String[] args) {
Baseball base = new Baseball();
base.bRandom();
base.loop();
base.resultPrint();
}
}
|
cs |
728x90
'JAVA' 카테고리의 다른 글
13일차// 초기화와 생성자 Constructor (0) | 2021.01.27 |
---|---|
12일차//SortingClass (0) | 2021.01.26 |
12일차// 객체 Class 사용하기(공부내용) (0) | 2021.01.26 |
[java] sorting 오름, 내림 method 사용 (0) | 2021.01.26 |
자바의 핵심 - 객체지향 프로그래밍(클래스와 객체)2-절차,객체 (0) | 2021.01.26 |