CODING TEST
백준 5613번 - 계산기 프로그램
우진하다
2023. 5. 22. 15:31
나의 풀이.
조건문을 사용해 풀 수 있는 문제였는데
스캐너의 nextInt()에 개행문자가 포함되어 에러를 ..
nextLine() 으로 받고 항변환 해주는 것으로 해결
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int firstNum = Integer.parseInt(sc.nextLine());
int result = firstNum;
String operator = sc.nextLine();
while (!operator.equals("=")) {
int nextNum = Integer.parseInt(sc.nextLine());
switch (operator) {
case "+":
result += nextNum;
break;
case "-":
result -= nextNum;
break;
case "*":
result *= nextNum;
break;
case "/":
result /= nextNum;
break;
default:
System.out.println("유효하지 않은 입력값 입니다.");
break;
}
operator = sc.nextLine();
}
System.out.println(result);
}
}