package algorithm;
import java.util.Scanner;
public class Baseball05 { // 숫자 개수 4개로
static int strike = 0;
static int ball = 0;
public static void main(String[] args) {
int count = 0; // 맞추기를 몇번시도했는지 (시도횟수)저장할 변수
String [] arr = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
String copy = "" ; // 랜덤한 4자리를 저장할 변수
String input = ""; // 4자리 수를 입력받는 변수
for (int x = 0; x < 100; x++) { // arr 배열 자리바꾸기 100번
int j = (int) (Math.random() * arr.length);
int k = (int) (Math.random() * arr.length);
String temp = "";
temp = arr[j];
arr[j] = arr[k];
arr[k] = temp;
}
//System.arraycopy(arr, 0, arrCopy, 0, arrCopy.length); // arr 의 앞4개 숫자를 arrCopy로 복사
for(int i = 0; i < 4; i++) { // arr 의 앞4개 숫자를 copy로 복사
copy+=arr[i];
}
for (int i = 0; i < 4; i++) {
System.out.println(copy.charAt(i));
}
do {
strike = 0;
ball = 0;
System.out.println("4개의숫자를 입력하세요");
Scanner s = new Scanner(System.in);
for (int i = 0; i < 4; i++) {
input += s.next();
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < copy.length(); j++) {
if (i==j && input.charAt(i) == copy.charAt(j)) {
++strike;
} else if (input.charAt(i) == copy.charAt(j)) {
++ball;
}
}
}
System.out.println(strike + "strike, " + ball + "ball 입니다.");
count++;
if(strike == 4) {
System.out.println("4 Strike 입니다." + count + "회 만에 맞췄습니다. ");
if (count < 5) {
System.out.println("매우 잘했습니다.");
} else if (count < 10 && count >= 5) {
System.out.println("잘했습니다.");
} else {
System.out.println("보통입니다.");
}
break;
}
} while (true);
}
}
'IT 잡다 > sorcecode' 카테고리의 다른 글
| 7장 연습문제 19 re (0) | 2015.04.07 |
|---|---|
| wordScramble2 (0) | 2015.04.06 |
| BaseballGame 소스코드 (0) | 2015.04.03 |
| BaseballGame02 (0) | 2015.04.03 |
| BaseballGame (0) | 2015.04.03 |


