Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
관리 메뉴

개발이 하고싶니?

[백준 5613번]계산기 프로그램(자바) 본문

코딩테스트 풀어보기

[백준 5613번]계산기 프로그램(자바)

차해:) 2024. 1. 31. 01:44

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<String> inPut = new ArrayList<>();


        while(true){
            String inputLine = br.readLine();
            inPut.add(inputLine);
            if (inputLine.equals("=")){
                break;
            }
        }

        int[] num = new int[inPut.size()/2];
        String[] sign = new String[inPut.size()/2];


        num[0] = Integer.parseInt(inPut.get(0));
        int numInDex = 1;
        int signInDex = 0;
        
        for (int i=1; i<inPut.size(); i++){
            if(i%2==0){
                num[numInDex]= Integer.parseInt(inPut.get(i));
                numInDex++;
            }else{
                sign[signInDex]= inPut.get(i);
                signInDex++;
            }
        }

        int value = num[0];

        for (int i = 0; i <num.length-1 ; i++) {
                if(sign[i].equals("+")){
                    value += num[i+1];
                } else if (sign[i].equals("-")) {
                    value -= num[i+1];
                } else if (sign[i].equals("*")) {
                    value *= num[i+1];
                } else if (sign[i].equals("/")) {
                    value /= num[i+1];
                }

        }

        System.out.println(value);

    }
}

 

for문을 벗어나지 못하네...;