-
Notifications
You must be signed in to change notification settings - Fork 8
(강상문) StringCalculator 리뷰 요청드립니다. #27
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: sangmoon/main
Are you sure you want to change the base?
Changes from 1 commit
d2ae7b4
33e2d0b
261ce04
c44c0c4
30ec2e9
dc2d1a5
684c9b9
7e09213
af42101
9d312d6
16f9805
352bab3
56aac5c
b32d31d
7a44f52
926d269
f8fe96c
2104618
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import java.util.Scanner; | ||
|
|
||
|
|
||
| public class Calculator { | ||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| String[] values = Input.readInput(scanner); | ||
| int result = Operator.calculate(values); | ||
| Output.printResult(result); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Input { | ||
| public static String[] readInput(Scanner scanner) { | ||
| System.out.print("수식을 입력하세요: "); | ||
| return scanner.nextLine().split(" "); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| public class Operations { | ||
| public int add(int a, int b) { | ||
| return a + b; | ||
| } | ||
|
|
||
| public int subtract(int a, int b) { | ||
| return a - b; | ||
| } | ||
|
|
||
| public int multiply(int a, int b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| public int divide(int a, int b) { | ||
| if (b == 0) { | ||
| throw new ArithmeticException("0으로 나눌 수 없습니다."); | ||
| } | ||
| return a / b; | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| public class Operator { | ||
| public static int calculate(String[] values) { | ||
|
||
| Operations operations = new Operations(); | ||
| int result = Integer.parseInt(values[0]); | ||
|
|
||
| for (int i = 1; i < values.length; i += 2) { | ||
| String operator = values[i]; | ||
| int nextNumber = Integer.parseInt(values[i + 1]); | ||
|
|
||
| switch (operator) { | ||
| case "+": | ||
| result = operations.add(result, nextNumber); | ||
| break; | ||
| case "-": | ||
| result = operations.subtract(result, nextNumber); | ||
| break; | ||
| case "*": | ||
| result = operations.multiply(result, nextNumber); | ||
| break; | ||
| case "/": | ||
| result = operations.divide(result, nextNumber); | ||
| break; | ||
| default: | ||
| System.out.println("잘못된 연산자입니다."); | ||
| return 0; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| public class Output { | ||
|
|
||
| public static void printResult(int result) { | ||
|
||
| System.out.println("결과: " + 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.
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.