Skip to content

[전서영] 계산기 구현 1차 #60

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요구사항에 메인 메소드를 사용하지 말라는 부분이 있었습니다!

//여기서부터 프로그램 시작
System.out.println("[전서영] 1주차 계산기 구현 과제");
// Scanner 객체 생성
Scanner scanner = new Scanner(System.in);

// 숫자 입력받기
System.out.println("Please enter the numbers. : ");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

숫자만 입력받는 형식이 아니기 때문에, "Please enter numbers separated by commas or colons (e.g., 1,2:3): " 와 같은 형식으로 안내되면 좋을 것 같아요!

String expression = scanner.nextLine();

// 쉼표와 콜론으로 숫자들 분리

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

계산 로직을 별개의 메소드로 분리하면 좋을 것 같아요!

String[] tokens = expression.split("[,|:]");

int sum = 0;
for (String token : tokens) {
sum += Integer.parseInt(token);
}

System.out.println("Results: " + sum);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

유효하지 않은 값이나, 음수 값이 입력되었을 경우 등에 대한 예외 처리가 필요할 것 같아요!

scanner.close();
}
}