내 답안
class Solution {
public int solution(String binomial) {
int answer = 0;
String[] parts = binomial.split(" ");
int a = Integer.parseInt(parts[0]);
String op = parts[1];
int b = Integer.parseInt(parts[2]);
if (op.equals("+")) {
answer = a + b;
} else if (op.equals("-")) {
answer = a - b;
} else if (op.equals("*")) {
answer = a * b;
}
return answer;
}
}
Java
복사
다른사람 풀이
public class Solution {
public static int solution(String binomial) {
String[] parts = binomial.split(" ");
int a = Integer.parseInt(parts[0]);
int b = Integer.parseInt(parts[2]);
char op = parts[1].charAt(0);
int result = 0;
switch (op) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
default:
throw new IllegalArgumentException("Invalid operator: " + op);
}
return result;
}
}
Java
복사
class Solution {
public int solution(String binomial) {
String[] cal = binomial.split(" ");
if (cal[1].equals("+")) {
return (Integer.parseInt(cal[0])) + (Integer.parseInt(cal[2]));
} else if (cal[1].equals("-")) {
return (Integer.parseInt(cal[0])) - (Integer.parseInt(cal[2]));
} else {
return (Integer.parseInt(cal[0])) * (Integer.parseInt(cal[2]));
}
}
}
Java
복사