Skip to content

Comments

[BE] μ „μ‹œν˜„ πŸ‘†μ—…λ‹€μš΄#3

Open
jj0526 wants to merge 1 commit intoLeets-Official:mainfrom
jj0526:sihyeon
Open

[BE] μ „μ‹œν˜„ πŸ‘†μ—…λ‹€μš΄#3
jj0526 wants to merge 1 commit intoLeets-Official:mainfrom
jj0526:sihyeon

Conversation

@jj0526
Copy link

@jj0526 jj0526 commented Apr 17, 2024

λ―Έμ…˜ μ‹€ν–‰ κ²°κ³Ό

image
image
image

κΈ°λŠ₯ λͺ…μ„Έμ„œ

get_random_number : 1-100의 숫자 쀑 1개λ₯Ό λžœλ€ν•˜κ²Œ return
get_random_alphabet : A - zκΉŒμ§€μ˜ μ˜μ–΄ μ•ŒνŒŒλ²³ 쀑 1개λ₯Ό λžœλ€ν•˜κ²Œ return
select_version : 1 숫자 λ˜λŠ” 2 μ˜μ–΄μ„ νƒ
- λ§Œμ•½ μˆ«μžκ°€ μ•„λ‹Œ κΈ€μžκ°€ 였면 μž…λ ₯ 문자 νƒ€μž… exception λ°œμƒ
- 1, 2κ°€ μ•„λ‹Œ 숫자라면 [ERROR] μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” λ²„μ „μž…λ‹ˆλ‹€ exception λ°œμƒ
checkNumberValidity(void) : μ§€κΈˆκΉŒμ§€ μ˜ˆμƒν•œ μˆ«μžλ“€μ„ μ΄μš©ν•œ λ²”μœ„μ™€ input 숫자λ₯Ό λΉ„κ΅ν•΄μ„œ λ²”μœ„ μ™Έλ©΄ exception λ°œμƒ

Comment on lines +63 to +152
void run() throws Exception {
System.out.println("버전을 μž…λ ₯ν•΄μ£Όμ„Έμš” (숫자 버전: 1, μ˜μ–΄ 버전: 2) : ");
Scanner sc = new Scanner(System.in);
int version = select_version();
int tryNum = 0;
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++;

}

}
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';
}
}
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 + "회");

}
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 μˆ«μžλ²„μ „κ³Ό μ•ŒνŒŒλ²³ λ²„μ „μ˜ 클래슀λ₯Ό λ”°λ‘œ λ§Œλ“€μ–΄μ„œ μž‘μ„±ν•˜λŠ” 것도 μ’‹μ•„λ³΄μž…λ‹ˆλ‹€! μˆ˜κ³ ν•˜μ…¨μ”€λ‹ˆλ‹€

Comment on lines +133 to +135
if(min == '[') {
min = 'a';
}
Copy link
Member

Choose a reason for hiding this comment

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

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

Comment on lines +48 to +54
if(version==1){
return 1;
}
else if (version==2){
return 2;
}
return 0;
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 λ³€μˆ˜λ₯Ό λ¦¬ν„΄ν•˜λŠ”κ²Œ 더 μ’‹μ•„λ³΄μž…λ‹ˆλ‹€.

Comment on lines +91 to +100
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++;
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을 좜λ ₯ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ”°λ‘œ λ§Œλ“€μ–΄μ„œ λΆ„λ¦¬ν•˜λŠ”κ±΄ μ–΄λ–¨κΉŒμš”?

Copy link
Member

@jiixon jiixon left a comment

Choose a reason for hiding this comment

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

λ‹€μŒ λ²ˆμ—λŠ” mvc ꡬ쑰둜 λΆ„λ¦¬ν•΄μ„œ κ΅¬ν˜„ν•˜λŠ”κ²Œ μ–΄λ–¨κΉŒμš”? κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€!

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λ₯Ό μ‚¬μš©ν•˜λ©΄ 쒋을 것 κ°™μ•„μš”!


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에 λ„£κΈ° λ³΄λ‹€λŠ” μ•žμ„œ λ§ν•œ λ°©λ²•μœΌλ‘œ ν•˜λŠ”κ²Œ 가독성이 쒀더 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€!

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λ₯Ό ν•˜κΈ°λ•Œλ¬Έμ— 차이가 μžˆμ„κ²ƒ κ°™μŠ΅λ‹ˆλ‹€!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants