-
Notifications
You must be signed in to change notification settings - Fork 9
[문자열 계산기] 이성준 미션 제출합니다. #10
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,7 +1,60 @@ | ||
| package calculator; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Console; | ||
| /*입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다. | ||
|
|
||
| - 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다. | ||
| - 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6 | ||
| - 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다. | ||
| - 예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. | ||
| - 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다.*/ | ||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
|
|
||
| // 변수 선언 | ||
| String stnc; // 입력값 | ||
| int answer = 0; | ||
| boolean hasCustomed = false; | ||
| char customId = ','; // 커스텀 구분자 | ||
|
|
||
|
|
||
| // 문자열 입력받기 | ||
| System.out.println("덧셈할 문자열을 입력해 주세요."); | ||
| stnc = Console.readLine(); | ||
|
|
||
| int i=0; | ||
|
|
||
| if(stnc.substring(0,2).equals("//") && stnc.substring(3,5).equals("\\n")){ | ||
| customId = stnc.charAt(2); | ||
| hasCustomed = true; | ||
| i=5; | ||
| } | ||
|
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. 정규 표현식 쓰면 더 쉽게 확인 및 추출할 수 있을 것 같아요!
Comment on lines
+28
to
+32
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 odr = i; | ||
| int tmp = 1; | ||
|
|
||
| for(; i<stnc.length(); i++){ | ||
| char num = stnc.charAt(i); | ||
| boolean condition= num == ',' || num == ':'; | ||
| if(hasCustomed){condition = condition || num == customId;} | ||
|
|
||
| if(i==stnc.length()-1){ // 마지막에는 구분자 이후의 숫자를 더함 | ||
| for(int j = i; j>=odr; j--){ | ||
| if(48 > num || num>57){throw new IllegalArgumentException();} | ||
| answer += (num-48) * tmp; | ||
| tmp *= 10; | ||
| } | ||
| }else if(condition){ // , 이나 : 나오면 그 전까지를 answer에 더함 | ||
| for(int j = i-1; j>=odr; j--){ | ||
| if(48 > num || num>57){throw new IllegalArgumentException();} | ||
| answer += (num-48) * tmp; | ||
| tmp *= 10; | ||
| } | ||
|
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. 이 부분도 substring으로 추출한 다음 바로 변환하는게 더 읽기 쉽지 않을까요? |
||
| tmp = 1; | ||
| odr = i+1; | ||
| } | ||
| } | ||
|
|
||
| System.out.println("결과 : " + answer); | ||
| } | ||
| } | ||
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.
변수명을 축약하지 않는게 더 좋을 것 같아요!