-
Notifications
You must be signed in to change notification settings - Fork 13
[BE] μ μν πμ λ€μ΄ #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(){ | ||
| 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄λ κ² λ―Έλ¦¬ μ νλκ² λ³΄λ€ "μ
λ ₯κ°μ΄ 1μ΄λ©΄, 2μ΄λ©΄" μΌλ‘ ꡬλΆν΄μ ꡬννλκ²μ μ΄λ¨κΉμ? |
||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
| 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); | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
snake caseλ§κ³ Camel caseλ₯Ό μ¬μ©νλ©΄ μ’μ κ² κ°μμ!