자바로 테스트코드 작성해봤습니다. 피드백 부탁드려요
```{.java}
import java.util.Random;
import java.util.Scanner;
import java.util.stream.IntStream;
/**
* 코딩도장 문제풀이
* @Title 숫자야구게임
* @Level 2
* @author jyh
* @Date 2022年08月09日
* @leadTime 2h 30m
*
*/
public class CountBaseBall {
public static final int DIGIT_NUM = 5; // 숫자야구게임 문제&정답숫자 자릿수
private static String[] digitStr = new String[DIGIT_NUM]; // 정답숫자
private static String[] inputStr = new String[DIGIT_NUM]; // 입력숫자
private static Scanner sc; // 입력객체
public static void main(String[] args) {
int[] num = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; // int형 배열
Random r = new Random();
String bf = null;
for (int i = 0; i < digitStr.length; i++) {
int idx = r.nextInt(num.length);
digitStr[i] = num[idx] + "";
num = remove(num, idx);
}
System.out.println(":::::::::::::::::::: BaseBall Game Start ::::::::::::::::::::");
try {
sc = new Scanner(System.in);
while (true) {
System.out.print("[0-9까지] 서로다른 "+DIGIT_NUM+"자리의 값을 입력하시오 : ");
bf = sc.nextLine();
if (bf != null && bf.length() == DIGIT_NUM) {
boolean isNumeric = bf.matches("[+-]?\\d*(\\.\\d+)?"); // 입력된 값 숫자여부 판단
if (isNumeric && dupCheck(bf)) {
bf = bf.substring(0, DIGIT_NUM);
System.out.println("입력한 답 : " + bf);
inputStr = bf.split("");
} else {
// 숫자로 다시입력
continue;
}
int strike = 0;
int ball = 0;
for (int j = 0; j < digitStr.length; j++) {
for (int k = 0; k < inputStr.length; k++) {
if (digitStr[j].equals(inputStr[k])) {
if (j == k) {
strike++;
} else {
ball++;
}
}
}
}
if (strike == digitStr.length) {
System.out.println("정답입니다!! 축하합니다!");
System.out.println(":::::::::::::::::::: BaseBall Game End ::::::::::::::::::::");
break;
} else {
System.out.println("아쉽습니다.. [ " + strike + "S" + ball + "B ] 입니다");
}
}
}
} catch (Exception e) {
// 예외처리
}
}
private static boolean dupCheck(String bf) {
String[] str = bf.split("");
for (int i = 0; i < str.length; i++) {
for (int j = (i+1); j < str.length; j++) {
if ( str[i].equals(str[j])) {
return false;
}
}
}
return true;
}
/**
* 숫자야구게임에서 중복된 숫자를 허용하지 않도록 idx 값 사용 후 제거하는 기능
* @param num int형 숫자배열
* @param idx 삭제할 index 값
* @return 필터링 된 num 값
*/
private static int[] remove(int[] num, int idx) {
return IntStream.range(0, num.length)
.filter(i -> i != idx)
.map(i -> num[i])
.toArray();
}
}
```
자바로 테스트코드 작성해봤습니다. 피드백 부탁드려요
```{.java}
import java.util.Random;
import java.util.Scanner;
import java.util.stream.IntStream;
/**
* 코딩도장 문제풀이
* @Title 숫자야구게임
* @Level 2
* @author jyh
* @Date 2022年08月09日
* @leadTime 2h 30m
*
*/
public class CountBaseBall {
public static final int DIGIT_NUM = 5; // 숫자야구게임 문제&정답숫자 자릿수
private static String[] digitStr = new String[DIGIT_NUM]; // 정답숫자
private static String[] inputStr = new String[DIGIT_NUM]; // 입력숫자
private static Scanner sc; // 입력객체
public static void main(String[] args) {
int[] num = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }; // int형 배열
Random r = new Random();
String bf = null;
for (int i = 0; i < digitStr.length; i++) {
int idx = r.nextInt(num.length);
digitStr[i] = num[idx] + "";
num = remove(num, idx);
}
System.out.println(":::::::::::::::::::: BaseBall Game Start ::::::::::::::::::::");
try {
sc = new Scanner(System.in);
while (true) {
System.out.print("[0-9까지] 서로다른 "+DIGIT_NUM+"자리의 값을 입력하시오 : ");
bf = sc.nextLine();
if (bf != null && bf.length() == DIGIT_NUM) {
boolean isNumeric = bf.matches("[+-]?\\d*(\\.\\d+)?"); // 입력된 값 숫자여부 판단
if (isNumeric && dupCheck(bf)) {
bf = bf.substring(0, DIGIT_NUM);
System.out.println("입력한 답 : " + bf);
inputStr = bf.split("");
} else {
// 숫자로 다시입력
continue;
}
int strike = 0;
int ball = 0;
for (int j = 0; j < digitStr.length; j++) {
for (int k = 0; k < inputStr.length; k++) {
if (digitStr[j].equals(inputStr[k])) {
if (j == k) {
strike++;
} else {
ball++;
}
}
}
}
if (strike == digitStr.length) {
System.out.println("정답입니다!! 축하합니다!");
System.out.println(":::::::::::::::::::: BaseBall Game End ::::::::::::::::::::");
break;
} else {
System.out.println("아쉽습니다.. [ " + strike + "S" + ball + "B ] 입니다");
}
}
}
} catch (Exception e) {
// 예외처리
}
}
private static boolean dupCheck(String bf) {
String[] str = bf.split("");
for (int i = 0; i < str.length; i++) {
for (int j = (i+1); j < str.length; j++) {
if ( str[i].equals(str[j])) {
return false;
}
}
}
return true;
}
/**
* 숫자야구게임에서 중복된 숫자를 허용하지 않도록 idx 값 사용 후 제거하는 기능
* @param num int형 숫자배열
* @param idx 삭제할 index 값
* @return 필터링 된 num 값
*/
private static int[] remove(int[] num, int idx) {
return IntStream.range(0, num.length)
.filter(i -> i != idx)
.map(i -> num[i])
.toArray();
}
}
```