Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 154 additions & 2 deletions src/main/java/leets/land/UpdownApplication.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,160 @@
package leets.land;
import java.util.InputMismatchException;
import java.util.Random;

import java.util.Scanner;

public class UpdownApplication {
public int get_random_number(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snake case말고 Camel caseλ₯Ό μ‚¬μš©ν•˜λ©΄ 쒋을 것 κ°™μ•„μš”!

Random r = new Random();

return r.nextInt(100)+1;
}
public char get_random_alphabet(){
Random r = new Random();
int randomChar = r.nextInt(52); // 0λΆ€ν„° 51κΉŒμ§€μ˜ λ‚œμˆ˜ 생성
if (randomChar < 26) {
// λŒ€λ¬Έμž 선택
return (char)('A' + randomChar);
} else {
// μ†Œλ¬Έμž 선택
return (char)('a' + randomChar - 26);
}
}

public int select_version() throws Exception {
Scanner sc = new Scanner(System.in);
int version = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ΄λ ‡κ²Œ 미리 μ •ν•˜λŠ”κ²ƒ 보닀 "μž…λ ₯값이 1이면, 2이면" 으둜 κ΅¬λΆ„ν•΄μ„œ κ΅¬ν˜„ν•˜λŠ”κ²ƒμ€ μ–΄λ–¨κΉŒμš”?
이미 μ •ν•΄μ§„ 값을 λ‹€μ‹œ version에 λ„£κΈ° λ³΄λ‹€λŠ” μ•žμ„œ λ§ν•œ λ°©λ²•μœΌλ‘œ ν•˜λŠ”κ²Œ 가독성이 쒀더 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€!

boolean valid = false;
while (!valid) {
try {
System.out.println("버전을 μ„ νƒν•˜μ„Έμš” (1 λ˜λŠ” 2): ");
String input = sc.nextLine();
version = Integer.parseInt(input);
if(version != 1 && version != 2) {
throw new Exception(); // 1κ³Ό 2 μ•„λ‹Œ μˆ«μžμ—¬λ„ μ˜ˆμ™Έ
}
valid = true; // 1λ˜λŠ” 2 μž…λ ₯
}
catch (NumberFormatException e) {
System.out.println("[ERROR] μž…λ ₯ 문자의 νƒ€μž…μ΄ λ§žμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
}
catch (Exception e) {
System.out.println("[ERROR] μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ²„μ „μž…λ‹ˆλ‹€.");
}

}

if(version==1){
return 1;
}
else if (version==2){
return 2;
}
return 0;
Comment on lines +48 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if else λ°˜ν™˜ 값을 μž…λ ₯받은 version λ³€μˆ˜λ₯Ό λ¦¬ν„΄ν•˜λŠ”κ²Œ 더 μ’‹μ•„λ³΄μž…λ‹ˆλ‹€.


}

void checkNumberValidity(int min, int max, int userInput) throws Exception {
if(userInput<min || userInput>max){
throw new Exception("[ERROR] λ²”μœ„ λ‚΄μ˜ 숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”.");
}
}
void run() throws Exception {
System.out.println("버전을 μž…λ ₯ν•΄μ£Όμ„Έμš” (숫자 버전: 1, μ˜μ–΄ 버전: 2) : ");
Scanner sc = new Scanner(System.in);
int version = select_version();
int tryNum = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1λΆ€ν„° μ‹œμž‘ν•˜κ±°λ‚˜ λ§ˆμ§€λ§‰μ— +1λ₯Ό ν•΄μ€˜μ•Όν•˜μ§€μ•Šμ„κΉŒμš”? up이후에 +1λ₯Ό ν•˜κΈ°λ•Œλ¬Έμ— 차이가 μžˆμ„κ²ƒ κ°™μŠ΅λ‹ˆλ‹€!

if(version == 1){
int answer = get_random_number();
int userNumber = -1;
int min = 1;
int max = 100;
while(answer != userNumber){
System.out.println("숫자λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” (" +min +" ~ "+max + ") : ");
try{
String input = sc.nextLine();
userNumber = Integer.parseInt(input);
}
catch (NumberFormatException e){
System.out.println("[ERROR] μž…λ ₯ 문자의 νƒ€μž…μ΄ λ§žμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
continue;
}
try {
checkNumberValidity(min, max, userNumber);

}
catch(Exception e){
System.out.println(e.getMessage());
continue;
}
if (answer > userNumber) {
System.out.println("UP");
min = userNumber + 1;
} else if (answer < userNumber) {
System.out.println("DOWN");
max = userNumber - 1;
} else {
System.out.println("μ •λ‹΅!");
}
tryNum++;
Comment on lines +91 to +100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up Down을 좜λ ₯ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ”°λ‘œ λ§Œλ“€μ–΄μ„œ λΆ„λ¦¬ν•˜λŠ”κ±΄ μ–΄λ–¨κΉŒμš”?


}

}
else if(version == 2){
char answer = get_random_alphabet();
char userAlphabet = ' ';
char min = 'A';
char max = 'z';
while(answer != userAlphabet){
System.out.println("μ˜μ–΄λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš” (" +min +" ~ "+max + ") : ");
try{
userAlphabet = sc.nextLine().charAt(0);
if(!Character.isLetter(userAlphabet)) {
throw new InputMismatchException(); // λ¬Έμžκ°€ μ•„λ‹Œ 경우 μ˜ˆμ™Έ λ°œμƒ
}
}
catch (InputMismatchException e){
System.out.println("[ERROR] μž…λ ₯ 문자의 νƒ€μž…μ΄ λ§žμ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
continue;
}
try {
checkNumberValidity(min, max, userAlphabet);

}
catch(Exception e){
System.out.println(e.getMessage());
continue;
}
if(answer>userAlphabet){
System.out.println("UP");
min = (char)(userAlphabet+1);
if(min == '[') {
min = 'a';
}
Comment on lines +133 to +135
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'['인 κ²½μš°λž‘ λΉ„κ΅ν•˜μ—¬ κ°™λ‹€λ©΄ μ΅œμ†Œκ°€ 'a'κ°€ λœλ‹€λŠ” 게 이해가 잘 μ•ˆλ˜λŠ” 것 κ°™μ•„μš”
μ•„μŠ€ν‚€μ½”λ“œλ₯Ό λͺ¨λ₯΄λŠ” μ‚¬λžŒμ΄ 이 μ½”λ“œλ₯Ό 읽으면 잘 이해할 수 μžˆμ„κΉŒμš”?

}
else if(answer<userAlphabet){
System.out.println("DOWN");
max = (char)(userAlphabet-1);
if(max == '`') {
max = 'Z';
}
}
else{
System.out.println("μ •λ‹΅!");
}
tryNum++;
}
}
System.out.println("μ‹œλ„ν•œ 횟수 : " + tryNum + "회");

}
Comment on lines +63 to +152
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

버전 1,2 μˆ«μžλ²„μ „κ³Ό μ•ŒνŒŒλ²³ λ²„μ „μ˜ 클래슀λ₯Ό λ”°λ‘œ λ§Œλ“€μ–΄μ„œ μž‘μ„±ν•˜λŠ” 것도 μ’‹μ•„λ³΄μž…λ‹ˆλ‹€! μˆ˜κ³ ν•˜μ…¨μ”€λ‹ˆλ‹€




public static void main(String[] args) {
System.out.print("hihi :D");
public static void main(String[] args) throws Exception {
UpdownApplication app = new UpdownApplication();
app.run();
}
}
19 changes: 19 additions & 0 deletions src/test/java/leets/land/UpdownApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package leets.land;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

class UpdownApplicationTests {
UpdownApplication app;

@BeforeEach
void setUp() {
app = new UpdownApplication();
}
@Test
void contextLoads() {
}
@Test
void checkNumberValidity(){
int min = 40;
int max = 60;
int userInput = 20;

assertThrows(Exception.class, ()->{
app.checkNumberValidity(min, max, userInput);
});

}

}