JAVA
7일차// [JAVA] Baseball 게임 , 야구게임
aesup
2021. 1. 19. 21:03
728x90
예시를 들자면,
Random user
7 1 6 - > 1 4 5 = 1 ball
-> 7 3 4 = 1 strike
-> 7 2 1 = 1 strike 1 ball
위치와 값이 맞아야 1 strike
위치는 다르고 값은 맞으면 1 ball
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
|
import java.util.Arrays;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
/*
Baseball
1~10 - > 노트에 아무숫자나 적어라 3개만
Random user
7 1 6 - > 1 4 5 = 1 ball
-> 7 3 4 = 1 strike
-> 7 2 1 = 1 strike 1 ball
1 2 2 -> 1 2 3
*/
// 1.Random -> 3개의 숫자(숫자가 다 달라야한다)
// rum1 != rum2 != rum3
///////////////////////////////////////loop
// 2. user input -> 3개 다입력 받은 후에
// 3. 비교
// 4. 메세지
//////////////////////////////////////loop
// 5. 결과출력
Scanner sc = new Scanner(System.in);
// 컴퓨터가 뿌리는 3개 수
int com[] = new int[3];
// 사용자가 입력하는 3개의 수
int num[] = new int[3];
int s = 0, b=0;
boolean randomball = true;
// 1.Random -> 3개의 숫자(숫자가 다 달라야한다)
while (randomball) {
for (int i=0; i<com.length; i++) {
com[i] = (int)(Math.random() * 9) + 1;
}
if (com[0] != com[1] && com[0] != com[2] && com[1] != com[2])
// 숫자를 만드는데 3자리 숫자가 다 다르게해서 while문을 탈출할 수 있도록
{
System.out.println(Arrays.toString(com)); //배열 내용출력, 초반에 import 반드시하기!!!!!!!!!!
randomball = false;
}
}
randomball = true;
System.out.println("숫자를 입력하세요: ");
// user 숫자입력 3번
while(true) {
for(int i=0; i<num.length; i++) {
System.out.print((i+1)+"번째 수: ");
num[i] = sc.nextInt();
if(num[i] > 9 || num[i] <= 0) {
System.out.println("숫자 범위는 1~9 입니다.");
break;
}
}
// strike 와 ball을 계산해준다.
for(int i=0; i<com.length; i++) {
for(int j=0; j<num.length; j++) {
if(com[i] == num[j] && i==j) { // 숫자와 위치가 일치
s++;
} else if(com[i] ==num[j] && i!=j) { // 위치는 다르나 숫자는 일치
b++;
}
}
}
System.out.println("s = "+ s + "b = " + b);
if(s == 3) {
System.out.println("정답입니다!!!");
break;
}
}
|
cs |
728x90