From 6354789e3114b78a112f471763b959b8a38ae4e0 Mon Sep 17 00:00:00 2001 From: zzu-yaaa Date: Thu, 18 Apr 2024 11:14:12 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EA=B2=8C=EC=9E=84=20=EB=B2=84?= =?UTF-8?q?=EC=A0=84=20=EC=84=A0=ED=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leets/land/UpdownApplication.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/leets/land/UpdownApplication.java b/src/main/java/leets/land/UpdownApplication.java index 09d7ae5..bbb4fa3 100644 --- a/src/main/java/leets/land/UpdownApplication.java +++ b/src/main/java/leets/land/UpdownApplication.java @@ -1,8 +1,28 @@ package leets.land; +import java.io.*; public class UpdownApplication { - public static void main(String[] args) { - System.out.print("hihi :D"); + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + public static void main(String[] args) throws Exception { + + //게임 시작 & 버전 입력 받기 + System.out.println("업다운 게임을 시작합니다.\n"); + System.out.print("버전을 입력해주세요 (숫자 버전: 1, 영어 버전: 2) : "); + Integer version = Integer.parseInt(br.readLine()); + if(version == 1){ + //숫자 버전 + + } + else if(version == 2){ + //영어 버전 + } + else{ + throw new Exception("[ERROR] 올바른 버전을 선택해주세요."); + } + + } + } From 26f93a41c4654dbf7475d45e814e037a73b19af4 Mon Sep 17 00:00:00 2001 From: zzu-yaaa Date: Thu, 18 Apr 2024 11:40:41 +0900 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90ver=20=EB=9E=9C?= =?UTF-8?q?=EB=8D=A4=20=EC=A0=95=EB=8B=B5=20=EC=83=9D=EC=84=B1=20=EB=B0=8F?= =?UTF-8?q?=20=EC=B6=9C=EB=A0=A5=20=ED=8F=AC=EB=A7=B7=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leets/land/UpdownApplication.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/main/java/leets/land/UpdownApplication.java b/src/main/java/leets/land/UpdownApplication.java index bbb4fa3..57ab8e9 100644 --- a/src/main/java/leets/land/UpdownApplication.java +++ b/src/main/java/leets/land/UpdownApplication.java @@ -1,6 +1,8 @@ package leets.land; import java.io.*; +import java.util.Random; + public class UpdownApplication { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); @@ -13,16 +15,54 @@ public static void main(String[] args) throws Exception { Integer version = Integer.parseInt(br.readLine()); if(version == 1){ //숫자 버전 - + UpdownNumber(); } else if(version == 2){ //영어 버전 + //UpdownEnglish(); } else{ - throw new Exception("[ERROR] 올바른 버전을 선택해주세요."); + throw new Exception("[ERROR] 존재하지 않는 버전입니다."); + } + + } + + private static void UpdownNumber() throws IOException { + + int min = 1; + int max = 100; + + // 랜덤 정답 생성 + Random random = new Random(); + int answer = random.nextInt(max - min + 1) + min; + + boolean isCorrect = false; + int tryCnt = 0; + while(!isCorrect){ + int userInput = getUserInput(min, max); + + if(userInput == answer){ + System.out.println("정답!"); + System.out.println("\n시도한 횟수 : "+tryCnt+"회"); + isCorrect = true; + } + else{ + checkRange(userInput); + } } + } + + //입력 조건을 확인면서 입력 받기 + private static int getUserInput(int min, int max) { + return 0; + } + + //사용자의 입력값을 판단하여 up or down 출력 + private static void checkRange(int userInput){ } + + } From e29e015950a4ab83c1c6fc7567a08271bb7dfe23 Mon Sep 17 00:00:00 2001 From: zzu-yaaa Date: Thu, 18 Apr 2024 11:50:58 +0900 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90ver=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=20=EC=9E=85=EB=A0=A5=EA=B0=92=20=EB=B0=9B?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leets/land/UpdownApplication.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/leets/land/UpdownApplication.java b/src/main/java/leets/land/UpdownApplication.java index 57ab8e9..85413ec 100644 --- a/src/main/java/leets/land/UpdownApplication.java +++ b/src/main/java/leets/land/UpdownApplication.java @@ -6,6 +6,7 @@ public class UpdownApplication { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static int tryCnt = 0; public static void main(String[] args) throws Exception { @@ -36,9 +37,10 @@ private static void UpdownNumber() throws IOException { Random random = new Random(); int answer = random.nextInt(max - min + 1) + min; + //정답 맞출때까지 게임 진행 boolean isCorrect = false; - int tryCnt = 0; while(!isCorrect){ + tryCnt += 1; int userInput = getUserInput(min, max); if(userInput == answer){ @@ -54,8 +56,39 @@ private static void UpdownNumber() throws IOException { } //입력 조건을 확인면서 입력 받기 - private static int getUserInput(int min, int max) { - return 0; + private static int getUserInput(int min, int max) throws IOException{ + int userInput = 0; + boolean isValidInput = false; + + while(!isValidInput){ + try{ + System.out.print("숫자를 입력해주세요(" + min + " ~ " + max + ") : "); + userInput = Integer.parseInt(br.readLine()); + + if(IsRightValue(min,max,userInput)){ + isValidInput = true; + } + else{ + tryCnt += 1; + System.out.println("[ERROR] 범위 내의 숫자를 입력하세요."); + } + } + catch (NumberFormatException e){ + tryCnt += 1; + System.out.println("[ERROR] 입력 문자의 타입이 맞지 않습니다."); + } + + } + + return userInput; + } + + //값이 범위 내에 있는지 확인 + private static boolean IsRightValue(int min, int max, int value) { + if (min <= value && value <= max){ + return true; + } + return false; } //사용자의 입력값을 판단하여 up or down 출력 From dc53a4039907b72f03844fb2690f4667bc361e04 Mon Sep 17 00:00:00 2001 From: zzu-yaaa Date: Thu, 18 Apr 2024 11:59:03 +0900 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=EC=88=AB=EC=9E=90ver=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=EC=9E=90=20=EC=9E=85=EB=A0=A5=EA=B0=92=20updown=20?= =?UTF-8?q?=ED=8C=90=EB=8B=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leets/land/UpdownApplication.java | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/leets/land/UpdownApplication.java b/src/main/java/leets/land/UpdownApplication.java index 85413ec..f04416c 100644 --- a/src/main/java/leets/land/UpdownApplication.java +++ b/src/main/java/leets/land/UpdownApplication.java @@ -7,6 +7,9 @@ public class UpdownApplication { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static int tryCnt = 0; + static int min = 1; + static int max = 100; + static int answer = 0; public static void main(String[] args) throws Exception { @@ -29,19 +32,15 @@ else if(version == 2){ } private static void UpdownNumber() throws IOException { - - int min = 1; - int max = 100; - // 랜덤 정답 생성 Random random = new Random(); - int answer = random.nextInt(max - min + 1) + min; + answer = random.nextInt(max - min + 1) + min; //정답 맞출때까지 게임 진행 boolean isCorrect = false; while(!isCorrect){ tryCnt += 1; - int userInput = getUserInput(min, max); + int userInput = getUserInput(); if(userInput == answer){ System.out.println("정답!"); @@ -56,7 +55,7 @@ private static void UpdownNumber() throws IOException { } //입력 조건을 확인면서 입력 받기 - private static int getUserInput(int min, int max) throws IOException{ + private static int getUserInput() throws IOException{ int userInput = 0; boolean isValidInput = false; @@ -65,7 +64,7 @@ private static int getUserInput(int min, int max) throws IOException{ System.out.print("숫자를 입력해주세요(" + min + " ~ " + max + ") : "); userInput = Integer.parseInt(br.readLine()); - if(IsRightValue(min,max,userInput)){ + if(IsRightValue(userInput)){ isValidInput = true; } else{ @@ -84,7 +83,7 @@ private static int getUserInput(int min, int max) throws IOException{ } //값이 범위 내에 있는지 확인 - private static boolean IsRightValue(int min, int max, int value) { + private static boolean IsRightValue(int value) { if (min <= value && value <= max){ return true; } @@ -93,7 +92,14 @@ private static boolean IsRightValue(int min, int max, int value) { //사용자의 입력값을 판단하여 up or down 출력 private static void checkRange(int userInput){ - + if(userInput > answer){ + System.out.println("DOWN"); + max = userInput-1; + } + else if(userInput < answer){ + System.out.println("UP"); + min = userInput+1; + } } From ae9456defb501b516ab1ee36fd1eb7b6ff7c2f8c Mon Sep 17 00:00:00 2001 From: zzu-yaaa Date: Thu, 18 Apr 2024 12:51:58 +0900 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20=EC=98=81=EC=96=B4ver=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leets/land/UpdownApplication.java | 116 ++++++++++++++---- 1 file changed, 94 insertions(+), 22 deletions(-) diff --git a/src/main/java/leets/land/UpdownApplication.java b/src/main/java/leets/land/UpdownApplication.java index f04416c..6c3319d 100644 --- a/src/main/java/leets/land/UpdownApplication.java +++ b/src/main/java/leets/land/UpdownApplication.java @@ -7,9 +7,8 @@ public class UpdownApplication { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static int tryCnt = 0; - static int min = 1; - static int max = 100; - static int answer = 0; + static int numMin = 1; static int numMax = 100; static int numAnswer = 0; + static char charMin = 'A'; static char charMax = 'z'; static char charAnswer; public static void main(String[] args) throws Exception { @@ -19,52 +18,51 @@ public static void main(String[] args) throws Exception { Integer version = Integer.parseInt(br.readLine()); if(version == 1){ //숫자 버전 - UpdownNumber(); + updownNumber(); } else if(version == 2){ //영어 버전 - //UpdownEnglish(); + updownEnglish(); } else{ - throw new Exception("[ERROR] 존재하지 않는 버전입니다."); + throw new IllegalArgumentException("[ERROR] 존재하지 않는 버전입니다."); } } - private static void UpdownNumber() throws IOException { + private static void updownNumber() throws IOException { // 랜덤 정답 생성 Random random = new Random(); - answer = random.nextInt(max - min + 1) + min; + numAnswer = random.nextInt(numMax - numMin + 1) + numMin; //정답 맞출때까지 게임 진행 boolean isCorrect = false; while(!isCorrect){ tryCnt += 1; - int userInput = getUserInput(); + int userInput = getUserInputNum(); - if(userInput == answer){ + if(userInput == numAnswer){ System.out.println("정답!"); System.out.println("\n시도한 횟수 : "+tryCnt+"회"); isCorrect = true; } else{ - checkRange(userInput); + checkRangeNum(userInput); } } - } //입력 조건을 확인면서 입력 받기 - private static int getUserInput() throws IOException{ + private static int getUserInputNum() throws IOException{ int userInput = 0; boolean isValidInput = false; while(!isValidInput){ try{ - System.out.print("숫자를 입력해주세요(" + min + " ~ " + max + ") : "); + System.out.print("숫자를 입력해주세요(" + numMin + " ~ " + numMax + ") : "); userInput = Integer.parseInt(br.readLine()); - if(IsRightValue(userInput)){ + if(IsRightValueNum(userInput)){ isValidInput = true; } else{ @@ -83,25 +81,99 @@ private static int getUserInput() throws IOException{ } //값이 범위 내에 있는지 확인 - private static boolean IsRightValue(int value) { - if (min <= value && value <= max){ + private static boolean IsRightValueNum(int value) { + if (numMin <= value && value <= numMax){ return true; } return false; } //사용자의 입력값을 판단하여 up or down 출력 - private static void checkRange(int userInput){ - if(userInput > answer){ + private static void checkRangeNum(int userInput){ + if(userInput > numAnswer){ System.out.println("DOWN"); - max = userInput-1; + numMax = userInput-1; } - else if(userInput < answer){ + else if(userInput < numAnswer){ System.out.println("UP"); - min = userInput+1; + numMin = userInput+1; + } + } + + private static void updownEnglish() throws IOException { + // 랜덤 정답 생성 + Random random = new Random(); + if (random.nextBoolean()) { + charAnswer = (char) (random.nextInt(26) + 'A'); // 대문자 A-Z + } else { + charAnswer = (char) (random.nextInt(26) + 'a'); // 소문자 a-z + } + + //정답 맞출때까지 게임 진행 + boolean isCorrect = false; + while(!isCorrect){ + tryCnt += 1; + char userInput = (char) getUserInputChar(); + + if(userInput == charAnswer){ + System.out.println("정답!"); + System.out.println("\n시도한 횟수 : "+tryCnt+"회"); + isCorrect = true; + } + else{ + checkRangeChar(userInput); + } + } + } + + private static int getUserInputChar() throws IOException{ + char userInput = 0; + boolean isValidInput = false; + + while(!isValidInput){ + System.out.print("알파벳을 입력해주세요 (" + charMin + " ~ " + charMax + ") : "); + String input = br.readLine(); + if(input.length() == 1){ + userInput = input.charAt(0); + if (userInput < 'A' || (userInput > 'z') || (userInput > 'Z' && userInput < 'a')) { + tryCnt += 1; + System.out.println("[ERROR] 입력 문자의 타입이 맞지 않습니다."); + } + else{ + if(IsRightValueChar(userInput)){ + isValidInput = true; + } + else{ + tryCnt += 1; + System.out.println("[ERROR] 범위 내의 숫자를 입력하세요."); + } + } + } + else{ + tryCnt += 1; + System.out.println("[ERROR] 한 글자만 입력하세요."); + } + } + return userInput; } + private static boolean IsRightValueChar(char userInput) { + if(charMin <= userInput && userInput <= charMax){ + return true; + } + return false; + } + private static void checkRangeChar(char userInput){ + if(userInput > charAnswer){ + System.out.println("DOWN"); + charMax = (char) (userInput-1); + } + else if(userInput < charAnswer){ + System.out.println("UP"); + charMin = (char) (userInput+1); + } + } }