-
Notifications
You must be signed in to change notification settings - Fork 9
[문자열 계산기] 김하빈 미션 제출합니다. #6
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 +1,14 @@ | ||
| # java-calculator-precourse | ||
| # java-calculator-precourse | ||
|
|
||
| ### 1. 구분자와 양수로 구성된 문자열 입력받기 | ||
|
|
||
| - 기본 구분자 문자열 입력 | ||
| - 커스텀 구분자 문자열 입력 | ||
|
|
||
| ### 2. 각 숫자의 합 반환 | ||
|
|
||
| - 커스텀 구분자 존재 여부 확인 | ||
| - 문자와 숫자 분리 | ||
| - 숫자 더하기 및 출력 | ||
|
|
||
| ### 3. IllegalArgumentException 발생 조건 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,53 @@ | ||
| package calculator; | ||
|
|
||
| import java.util.Scanner; | ||
| import java.util.Vector; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| Scanner sc = new Scanner(System.in); | ||
| Vector<Integer> number = new Vector<>(); | ||
|
Contributor
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. 백터를 쓰신 이유가 있을까요? 이유가 없다면 Vector 외에도 다른 선택지가 많을 것 같아요! 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. Vector말고 ArrayList로 불필요한 동기화를 줄여서 성능도 향상시켜보면 어떨까용? 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. 저도 덕분에 자바의 여러 중요한 부분들을 공부하게된 것 같아 좋네요 :) |
||
| String words; | ||
| char custom; | ||
|
|
||
| // 문자열 입력 | ||
| System.out.println("덧셈할 문자열을 입력해 주세요."); | ||
| words = sc.next(); | ||
|
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. nextLine()대신 공백기준으로 한단어만 입력받는 next()를 사용하신 이유가 있을까용? |
||
|
|
||
| // 커스텀 구분자 확인 | ||
| if(words.charAt(0)=='/' && words.charAt(1)=='/' && words.charAt(3)=='\\' && words.charAt(4)=='n') { | ||
|
Contributor
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. 여기도 정규표현식 쓰면 간결해질 것 같아요! 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. words의 index 2부분에만 구분자를 받을 경우, 스터디장님이 입력한 예외케이스를 보면 구분자를 여러문자로 입력했을 때 예외 처리 문제가 있을 것 같습니당 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. 맞네요 생각 못했어요 |
||
| custom = words.charAt(2); | ||
|
|
||
| // 숫자와 문자 분리 | ||
| for(int i=5; i<words.length(); i++) { | ||
| char word = words.charAt(i); | ||
| if(word >= 48 && word <= 57) { | ||
|
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. 아마 숫자인지를 확인하기위해 ASCII값을 직접 비교하는 로직을 넣으신 거겠죠..? 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. 동의합니다! |
||
| number.add(word-48); | ||
| } | ||
| else if(word != custom) { | ||
| // 예외 처리를 위한 예비출력 | ||
| System.out.println("예외"); | ||
| } | ||
| } | ||
|
Contributor
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. Character 단위로 하면 1의자리를 제외한 숫자는 표현이 안되지 않을까요...? ex) 입력 : 32 -> 결과 : 5 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. 동의합니다 |
||
| } | ||
| else { // 기본 구분자의 숫자와 문자 분리 | ||
| for(int i = 0; i < words.length(); i++) { | ||
| char word = words.charAt(i); | ||
| if(word >= 48 && word <= 57) { | ||
| number.add(word-48); | ||
| } | ||
| else if(word != ',' && word != ':') { | ||
| // 예외 처리를 위한 예비출력 | ||
| System.out.println("예외"); | ||
| } | ||
| } | ||
|
Contributor
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. 이렇게 순회하면서 number 이라는 백터에 담을 수도 있지만 split을 통해 쪼개는 것이 더 간단할 것 같아요! 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. 동의합니다. |
||
| } | ||
|
|
||
| // 숫자의 합 출력 | ||
| int sum = 0; | ||
| for(int i = 0; i < number.size(); i++) { | ||
|
Contributor
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. stream API와 Reduce를 써도 좋을 것 같아요! for문을 쓰더라도 |
||
| sum += number.get(i); | ||
| } | ||
| System.out.println(sum); | ||
| } | ||
| } | ||
|
Contributor
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. 주석 다셨던 단위로 함수를 분리하면 코드의 흐름이 한눈에 들어올 것 같아요! 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. 동의합니다 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. 동의합니다 |
||
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.
다음에는 미션에서 적혀있는대로
camp.nextstep.edu.missionutils에서 제공하는 Console을 사용해서 입력을 구현해주세요!