* 야구 게임
> 배열 / Scanner / 반복문(for, while) / Math.random
|
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
|
package day06quiz;
import java.util.Scanner;
public class Day06Q09 {
public static void main(String[] args) {
int[] com = new int[3];
int[] user = new int[3];
// 1. 컴퓨터가 세 자리 숫자를 생성한다.
// 0<= (int)Math.random()*10 < 1*10
for(int i = 0; i<com.length; i++) {
com[i] = (int)(Math.random()*10);
System.out.print(com[i]);
}
System.out.println();
// 2. 사용자로부터 3자리 숫자를 입력 받는다.
// 자동 import : ctrl + shift + o
Scanner sc = new Scanner(System.in);
// 이 부분부터 다시 반복
boolean isNotGameOver = true;
int cnt = 0;
while (isNotGameOver) {
cnt++;
System.out.println("입력 : ");
int userInput = sc.nextInt(); // 381/100
user[0] = userInput/100; // 3
user[1] = userInput%100/10; // 8
user[2] = userInput%10; // 1
// 3. 판별한다.
int strike = 0;
int ball = 0;
for(int i=0; i<3; i++) {
if(com[i] == user[i]) strike ++;
else {
for(int j=0; j<3; j++) {
if(user[i] == com[j]) ball ++;
}
}
}
if(strike == 3) {
System.out.println("정답 축하합니다. " +cnt+"회 만에 성공하셨습니다.");
isNotGameOver = false;
} else {
System.out.println("출력 : "+strike+"S"+ball+"B");
}
} // while end
} // main method end
} // class end
|
cs |
'Java' 카테고리의 다른 글
| [Java] Method 메서드 ( 오버로딩 overloading / 매개변수 / 가변인자 ) + 응용 (0) | 2023.03.13 |
|---|---|
| [Java] Method 메서드 ( return / overloading / call by value / call by reference) (0) | 2023.03.10 |
| [Java] 로또 번호 생성기 (배열 / for문 / Math.random 활용) (0) | 2023.03.09 |
| [Java] 배열 (1~3차원 배열 / 임시변수 - 변수값 교환 / 버블 소트) (0) | 2023.03.09 |
| [Java] 잔돈 교환기 및 자판기 Vending Machine (Scanner / if ~ else if 문 활용) (0) | 2023.03.09 |