💡풀이방안
"-" 를 기준으로 "+" 기호가 포함된 식으로 변경하여 배열에 삽입 후 "-" 제외한 식 계산 후 차례대로 "-" 연산 해주면 됨
EX) {'55', '-', '50+40'} -> {'55', '90'} -> 55-90
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String sen = bf.readLine();
ArrayList<String> arr = new ArrayList<>();
ArrayList<Integer> sum = new ArrayList<>();
int start = 0;
for (int i = 0; i < sen.length(); i++){
if (sen.charAt(i) == '-'){
arr.add(sen.substring(start,i));
arr.add(Character.toString(sen.charAt(i)));
start = i+1;
}
}
arr.add(sen.substring(start,sen.length()));
arr.removeAll(Arrays.asList("-"));
for (int i = 0; i<arr.size() ; i++){
int answer = 0;
int start2 = 0;
if (!arr.get(i).contains("+")){
answer += Integer.parseInt(arr.get(i));
}else{
for (int j = 0; j < arr.get(i).length(); j++){
if (arr.get(i).charAt(j) == '+'){
answer += Integer.parseInt(arr.get(i).substring(start2,j));
start2 = j+1;
}
}
answer += Integer.parseInt(arr.get(i).substring(start2,arr.get(i).length()));
}
sum.add(answer);
}
int result = sum.get(0);
for (int i =1; i< sum.size(); i++){
result -= sum.get(i);
}
System.out.println(result);
}
}