From fd4a6107f16ce7c5966a9ec1ed5b843bf6bb2979 Mon Sep 17 00:00:00 2001 From: eunsoni <135586807+eunsoni@users.noreply.github.com> Date: Mon, 6 May 2024 08:33:10 +0900 Subject: [PATCH 01/10] Update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d7e8aee..7c7e33fe 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# java-baseball-precourse \ No newline at end of file +# java-baseball-precourse +기능구현 +1. 난수 생성 +2. 사용자에게 3자리 숫자 입력받기 +3. 정답이 아닐 시 입력된 숫자 스트라이크, 볼 알려주고 재입력받기, 정답일 경우 종료 From f076ee7a22564d3e9c6cc610226a4083b640780b Mon Sep 17 00:00:00 2001 From: eunsoni Date: Mon, 6 May 2024 09:14:25 +0900 Subject: [PATCH 02/10] create NumberGenerator class --- src/main/java/NumberGenerator.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/main/java/NumberGenerator.java diff --git a/src/main/java/NumberGenerator.java b/src/main/java/NumberGenerator.java new file mode 100644 index 00000000..2a73025a --- /dev/null +++ b/src/main/java/NumberGenerator.java @@ -0,0 +1,17 @@ +public class NumberGenerator { + public static int[] generateUniqueRandomNumbers(int length) { + int[] numArr = new int[length]; + + for (int i = 0; i < numArr.length; i++) { + numArr[i] = (int) (Math.random() * 9 + 1); + for (int j = 0; j < i; j++) { + if (numArr[j] == numArr[i]) { + i--; + break; + } + } + } + + return numArr; + } +} From f6c2cd6a2c919903a3945c3a8d5db8c807f77c9d Mon Sep 17 00:00:00 2001 From: eunsoni Date: Mon, 6 May 2024 09:36:28 +0900 Subject: [PATCH 03/10] create InputNumber class --- src/main/java/InputNumber.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/main/java/InputNumber.java diff --git a/src/main/java/InputNumber.java b/src/main/java/InputNumber.java new file mode 100644 index 00000000..4a3bed5a --- /dev/null +++ b/src/main/java/InputNumber.java @@ -0,0 +1,30 @@ +import java.util.Scanner; + +public class InputNumber { + public static int[] inputNum(int[] inputArr) { + boolean isDuplicate = false; + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("숫자를 입력해주세요 : "); + String input = scanner.next(); + for (int i = 0; i < inputArr.length; i++) { + inputArr[i] = Character.getNumericValue(input.charAt(i)); + for (int j = 0; j < i; j++) { + if (inputArr[j] == inputArr[i]) { + System.out.println("중복된 값을 입력했습니다."); + isDuplicate = true; + break; + } + } + if (isDuplicate) { + isDuplicate = false; + break; + } + } + if (!isDuplicate) { + break; + } + } + return inputArr; + } +} From 9e8bd81fde8a54f09658dd9f605774618efa4e52 Mon Sep 17 00:00:00 2001 From: eunsoni Date: Mon, 6 May 2024 09:54:26 +0900 Subject: [PATCH 04/10] update InputNumber class, add Exception function --- src/main/java/InputNumber.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/InputNumber.java b/src/main/java/InputNumber.java index 4a3bed5a..35c1be72 100644 --- a/src/main/java/InputNumber.java +++ b/src/main/java/InputNumber.java @@ -5,8 +5,14 @@ public static int[] inputNum(int[] inputArr) { boolean isDuplicate = false; Scanner scanner = new Scanner(System.in); while (true) { - System.out.println("숫자를 입력해주세요 : "); + System.out.print("숫자를 입력해주세요 : "); String input = scanner.next(); + + // 입력된 숫자가 세 자리가 아닌 경우 IllegalArgumentException 발생 + if (input.length() != 3) { + throw new IllegalArgumentException("세 자리 숫자를 입력해주세요."); + } + for (int i = 0; i < inputArr.length; i++) { inputArr[i] = Character.getNumericValue(input.charAt(i)); for (int j = 0; j < i; j++) { From 324d99c6635dc1ce826079cf4f04e650e255cef0 Mon Sep 17 00:00:00 2001 From: eunsoni Date: Mon, 6 May 2024 10:05:26 +0900 Subject: [PATCH 05/10] create Check class --- src/main/java/Check.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/Check.java diff --git a/src/main/java/Check.java b/src/main/java/Check.java new file mode 100644 index 00000000..f60f0005 --- /dev/null +++ b/src/main/java/Check.java @@ -0,0 +1,24 @@ +public class Check { + public static boolean checkAnswer(int[] numArr, int[] inputArr) { + int strike = 0; + int ball = 0; + + for (int i = 0; i < numArr.length; i++) { + for (int j = 0; j < inputArr.length; j++) { + if (numArr[i] == inputArr[j] && i == j) { + strike++; + } else if (numArr[i] == inputArr[j] && i != j) { + ball++; + } + } + } + + if (strike == 3) { + System.out.println("정답!"); + return true; + } else { + System.out.println(strike + "스트라이크 " + ball + "볼"); + return false; + } + } +} From 61880406c95c9b66639a3206f6b7c77c941c8dd2 Mon Sep 17 00:00:00 2001 From: eunsoni Date: Mon, 6 May 2024 10:07:30 +0900 Subject: [PATCH 06/10] create Main class --- src/main/java/Main.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/Main.java diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 00000000..7f7faa0c --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,29 @@ +import java.util.Scanner; +public class Main { + public static void main(String[] args) { + int strike = 0; + int ball = 0; + + int[] numArr = NumberGenerator.generateUniqueRandomNumbers(3); + + Scanner scanner = new Scanner(System.in); + while (true) { + int[] inputArr = new int[3]; // 숫자를 저장할 배열 + + inputArr = InputNumber.inputNum(inputArr); + if (Check.checkAnswer(numArr, inputArr)) { + System.out.println("게임을 새로 시작하려면 1, 종료하려면 2를 입력하세요."); + int choice = scanner.nextInt(); + if (choice == 1) { + numArr = NumberGenerator.generateUniqueRandomNumbers(3); + continue; // 게임 새로 시작 + } else if (choice == 2) { + break; // 게임 종료 + } else { + System.out.println("잘못된 입력입니다. 게임을 종료합니다."); + break; + } + } + } + } +} \ No newline at end of file From 342a7fc624245aed2bb9ba94817e48eb3790ca9e Mon Sep 17 00:00:00 2001 From: eunsoni Date: Mon, 6 May 2024 10:19:37 +0900 Subject: [PATCH 07/10] update Check class, content of print --- bin/main/.gitkeep | 0 bin/main/Check.class | Bin 0 -> 1307 bytes bin/main/InputNumber.class | Bin 0 -> 1346 bytes bin/main/Main.class | Bin 0 -> 1263 bytes bin/main/NumberGenerator.class | Bin 0 -> 624 bytes bin/test/.gitkeep | 0 src/main/java/Check.java | 7 ++++--- 7 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 bin/main/.gitkeep create mode 100644 bin/main/Check.class create mode 100644 bin/main/InputNumber.class create mode 100644 bin/main/Main.class create mode 100644 bin/main/NumberGenerator.class create mode 100644 bin/test/.gitkeep diff --git a/bin/main/.gitkeep b/bin/main/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/bin/main/Check.class b/bin/main/Check.class new file mode 100644 index 0000000000000000000000000000000000000000..688a9021c95f196fafb6a74b67dbe331c10fcafb GIT binary patch literal 1307 zcmaJ=-*4Mg6#i~($4Qp1C7ss|x{+=jBrP;Rp+K`%+9HhwW&BX4BGD(;wN^KY9of#H zh!>{8OV5k%H0R{{-Rr>?b{*pQU2PQQ5>_l3pR><-_=Y03vbHDT5d;flN z{{es*%&3S7D5WLSsHuX-dVQ;?8M zLqS}izhc?u2d&0}>0Z_s>ZU-Z;uv~;PIs*^?t!wuWO)K=CEBbgaL5REJ!gBLnJ)3% ze0jc{|1^o`FsLH)43+AN=bMc*jzA*~r$ts;C1Pqhg^ygz_N%^Y>WyNCT@9si495kA z8hXtvIkutupIH79JL0n$Y>vg&o z|7C%`s;?WhOM0^>d8+EPT*JIzg@=-gbe{-2qqgTA$7dzIdCByboJB8%*YJjl*9AtO z-T=$K>eS3aSDvnt7j(mS+?7z`Nr78?CFo^$3trUi#k%Q@Rvf3+Y8Lk^wFh}-;pLU4 zc|iL7p8W2`EbcwQUWpRrs9L^R?2n_6z!VQjbk=U~uKg9P?{wCG-~DPkD}vv41kz>O zHr-NP_dL^6@vgv`eSII)i3;A_lmDN})9bd45gw+*D+$Et@wISN85L*+tupo}62C^% zDURVZ_#fh!?k3RoF-qbv`iXF{D6nLl*q73bG%HQXTPR6W$`&TaG{PdJ?O>k32n@+Dv1No`P|%(V#v-N(k`}6ftxp k_87^>BqHGzPR8&mCTI`Sdy|FV>Wyy`OEG?jrOx8`e^pmTF8}}l literal 0 HcmV?d00001 diff --git a/bin/main/InputNumber.class b/bin/main/InputNumber.class new file mode 100644 index 0000000000000000000000000000000000000000..6364bd28f6da9eeb4fbd66e5e2299587642bcee9 GIT binary patch literal 1346 zcmaJ=-*4Mg6#i~v$EoLRX05?svZP-S7VP z&o55_oWgkt0ft>UyIOZ=>g5H)ksvTcm$VPHjHTJ7%NNTe*qme9ZW}C&IKu&N<$Le;YV-b+xOe+n z^UnQ0zxdhv_KCOr*t_+4{A66l5DrRsk)dasPzQ#BmvD$dta4cD)28I>PF+m9G?pxK z(g_7gq)2t!xI%@!iDZj>XQhl`q$P|ztBjALAcI#)Sdl+Wi(>f(zw%od$?rzA!~SUwgh9}`(47PV^hH;SR*9kvy&1j5)H=dH`oCpb`ESsMU9>8#0(wU% z>cVd9p$u(ivk<5fxEdVaK=i^x#MZHI`XTyb2O1b0Zs6sakp^A~eS^pb4qu2JUB_#` z21kW8Or=y|L=_r%{V~R7gQMaa22!e+rgZuaq8wtffvhmThRCwWR8bY~ZvEc>CBi{J zB`{S;J-xfN(SK{}M(TmjmGeG_URp&OGDK{{P>F9JF(0C6n4EqD-DH3mom&s)sP=uT zxJVcK1Im@KAD5}RjDA$9_G1j-6WYl&m}t$3vv`Z50Oj9Cj#P-ChIjBTW%_Ulc}$aL x8H#7XwK+`}aE`u2yhpnRA^(jClMvm+fP~>qh-?!vwm0w>LSeoT;5i?@@Gt12PC)*vf zwIv}b7n*2-2Air7f_F9s8X^7w@Aw56~H(y zNTdX`Y0LE_G=cUj)<;&UW_gRH3kz2qI}k9&T+aUvT_)t%aH+&{1jFR&CDmU3K3vYQd7*BwIzr8YNGiyFgcQzOoy$8u+faINCRt z!vSL#AT(*h}_ET8Cd8>O z_*jTi|16G5JSWf=`zE?fJdYOyGOnVdiODF=E1fwM(J#?Q-IGg>y=>wo9HXzTE)!n! zW5*Ye#1j31Qa@m#gqK;7MvDd76*|l8{U2|(-3T{sg?E203?3^)-(L-XxV~!|3U5CwMC(_>joUOvt6R}GUk>E(Do#qA5a^5( z%Czi>X5uxxPK_%{KuslYCpB>=?Wt-PMof%ij9Ab4Bq)2-&%P>Wzy>=Jt=|ZLd=P$4 znQ?fvu3Zb)ZVeRnd}K%N-JK`4lI1V5=$()0B<8ruo?2#CiZYQ4R;@;x*Q!tWOloSL zM%hgIf(%JL_cjYVH7Qt(H|(W`Ym;nWHL&dEX=^3!Zi&+pQv$haqvhMqIaf_VRyF#7 zN)~#-Uvl1n6i=KQu9CMBcfnonzmLpM@pF!=dREXju4V$@zK^#op1^)u=qw7PR3w+u zW9e~Y3+*!x&~<4OhwkH<5zTm*8_~P zq7&yir_)5u;vnAVSgFsvnFuE4G;b-qhY51CEVzhCo>JtUrS}x$=DD9Ei^eFY=sCsM o!{|YoI0J9dzX#ejVYn^F_Wf->lppgW9rWXYKsp4$Wc(nC1jY%G`22^0m7`dBX9wzK zs?~Uzlu=`3;u<^FVgE+f!FBnCqU4RAu##ikV9CJNU+R-|X8-^I literal 0 HcmV?d00001 diff --git a/bin/test/.gitkeep b/bin/test/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/Check.java b/src/main/java/Check.java index f60f0005..2fba2f29 100644 --- a/src/main/java/Check.java +++ b/src/main/java/Check.java @@ -14,10 +14,11 @@ public static boolean checkAnswer(int[] numArr, int[] inputArr) { } if (strike == 3) { - System.out.println("정답!"); - return true; - } else { System.out.println(strike + "스트라이크 " + ball + "볼"); + System.out.println("3개의 숫자를 모두 맞히셨습니다! 게임 종료"); + return true; + } else {System.out.println(strike + "스트라이크 " + ball + "볼"); + return false; } } From be5811035690e4797f62874da86a2873fb0977bb Mon Sep 17 00:00:00 2001 From: eunsoni Date: Mon, 6 May 2024 10:40:26 +0900 Subject: [PATCH 08/10] update InputNumber class, depth --- bin/main/InputNumber.class | Bin 1346 -> 1369 bytes src/main/java/InputNumber.java | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/bin/main/InputNumber.class b/bin/main/InputNumber.class index 6364bd28f6da9eeb4fbd66e5e2299587642bcee9..bbae03e4dd2de0d704db7dcddebcdf8ffeae6eee 100644 GIT binary patch delta 360 zcmXAkOD{uV5Qd-6xpA5%bvcqcA}mDGln70mw6ze|qUzEjE|C_YVIghPbR#zYL09ao zY_Sj#>B5p<;Ahx2sKH_;Gw)=ccjm=@x9!*Ory4NE`klR~g*^ND!+dF~k}sBuil(hi zh004L6vp33;ax3d_l}d<({wg>a$u3F7PNB90y>s}D%B4yxZ$&(^{B4JwXktRDBu`_ zj`2X`kyuKap|I)K9dq;&-#u>I_yZGq{D$^lQBPkO$}s}9>&BaV)wl}W3RJ_EyhwZX zOn1*)8KI339Yk@Mp_3$jGIX(n%P!sI=;27_1^Ot{k4Ls=|IcGuHlRuLSrW)Q95ctf zC|zu`AS_Mzlj4?=mm*&3h8(7jagi2T1J+tYjg;9b`(aI7b zR&ZFQjWuCAbWkA70WL*sPKa?#o-u_~sj5>)M;Z@m6`6UR)$Kl=w(0X) Date: Mon, 6 May 2024 10:46:39 +0900 Subject: [PATCH 09/10] update NumberGenerator class, depth --- bin/main/NumberGenerator.class | Bin 624 -> 643 bytes src/main/java/NumberGenerator.java | 16 +++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/bin/main/NumberGenerator.class b/bin/main/NumberGenerator.class index cded7f544df77582c249737c3722bdb3abfb8799..e73833f3f9502910ddf6cffce4664748caa01aac 100644 GIT binary patch delta 275 zcmXAiy-vb#6vco4a-pG8tce&YRz%cNT@&Y|awq>G_Tid&@rxaeM*6j?=Ioe~-KTtd}qc+6?4_WzM(@&|ef qyBaNEw6enr0hY!ptdiGpp9PA_gf-2&SW~h#C7X3q)Jj=Ji;Z6v)+-hO delta 256 zcmZo>{lLO?>ff$?3=9mm3`!fh*cc@lGZ`5evltn8gG&;Vvwaf_LK2g5QrQ`#Cp$A% z)gNMDV_*TPmfFMR!)&{pL0?;D2ZMkJgIG3ya-To5-Esy|ZCO2S8NCGzoGncMmosp; zFf+=^Ed2kbg^5u{d*8zU7qvG44Pa*A0+L!_k{M_uD+4FcXfXz$MGPDaDnOn(P)-ZT zHv-C8Gw?HT1J(O!L2PG`V2}iISr{xCq<|V2fm%%&WPoB!Kp80pSs;s Date: Mon, 6 May 2024 10:57:56 +0900 Subject: [PATCH 10/10] Update README.md --- README.md | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7c7e33fe..a157d7fe 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,21 @@ # java-baseball-precourse -기능구현 -1. 난수 생성 -2. 사용자에게 3자리 숫자 입력받기 -3. 정답이 아닐 시 입력된 숫자 스트라이크, 볼 알려주고 재입력받기, 정답일 경우 종료 +<기능구현> +#1. 난수 생성 +-서로 다른 정수 3개로 이루어진 난수 생성 +#2. 사용자에게 3자리 숫자 입력받기 +-중복된 수가 2개이상 들어가있을 경우 알려주고 다시 입력받기 +-3자리보다 적거나 많은 수 입력 시 IllegalArgumentException 을 발생시킨 후 애플리케이션은 종료하기 +#3. 입력된 숫자 체크하기 +-스트라이크: 자리와 숫자가 모두 일치 +-볼: 자리는 맞지않지만 숫자 일치 +-입력할때마다 스트라이크, 볼의 수를 알려주기 +-정답이 아닐 경우 계속 입력받기 +-정답일 경우 1을 입력하면 새로운 게임 다시 시작하고 2를 입력하면 애플리케이션 종료하기 + + +#InputNumber.java: 사용자로부터 입력값을 받는 역할 +#NumberGenerator.java: 정답을 생성하는 역할 +#Check.java: 입력값에 대한 결과를 사용자에게 알려주고 그 다음 수행을 결정한다. +#Main.java: 시작클래스 + +