-
Notifications
You must be signed in to change notification settings - Fork 4
DaeunLee #2
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?
DaeunLee #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,38 @@ | ||
| package calculator; | ||
|
|
||
| /* | ||
| 1주차에만 제공되는 예시 코드입니다. | ||
| 코드는 그대로 사용해도 되고 수정해도 됩니다. | ||
| */ | ||
| import java.util.Objects; | ||
|
|
||
| public class Calculator { | ||
| public int calculate(String input) { | ||
| // TODO: 코드 구현 | ||
| throw new IllegalArgumentException("아직 구현되지 않았습니다."); | ||
| if(input.isEmpty()){ | ||
| return 0; | ||
| } | ||
| int result=0; | ||
| int num=0; | ||
| char b='+'; | ||
| for(int i=0;i<input.length();i++){ | ||
| char a=input.charAt(i); | ||
| boolean isNum = Character.isDigit(a); | ||
|
|
||
| if(isNum){ | ||
| num=a-'0'; | ||
| switch(b){ | ||
| case '+': result+=num; break; | ||
| case '-': result-=num; break; | ||
| case '*': result*=num; break; | ||
| case '/': | ||
| if(num==0){ | ||
| throw new IllegalArgumentException("0 입력 불가능"); | ||
| } | ||
| result/=num; break; | ||
| } | ||
|
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. 스위치를 활용해 깔끔하게 처리한 부분과 0으로 나눌 때 에러 처리한 부분이 좋네요. |
||
| } | ||
| else if(a=='+' || a=='-' || a=='*' || a=='/'){ | ||
| b=a; | ||
| } | ||
| throw new IllegalArgumentException("잘못된 수식 입력"); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
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.
b하는 변수가 무슨 역할을 하는지 이름을 자세하게 지어주시면 좋을 것 같아요!