[Java] 19. 조건 문자열

김주희's avatar
May 23, 2025
[Java] 19. 조건 문자열

1. 문제 설명, 제한 사항, 입출력 예시

notion image
notion image
 

2. 아이디어

💡
조건에 따라 분기
 

3. 풀이 코드

class Solution { public int solution(String ineq, String eq, int n, int m) { if (ineq.equals(">")){ if(eq.equals("=")){ if(n >= m){ return 1; }else return 0; }else{ if(n > m){ return 1; }else return 0; } }else{ if(eq.equals("=")){ if(n <= m){ return 1; }else return 0; }else{ if(n < m){ return 1; }else return 0; } } } }
class Solution { public int solution(String ineq, String eq, int n, int m) { String operator = ineq+eq; int answer; if(operator.equals(">=")){ return answer = n >= m ? 1 : 0; }else if(operator.equals(">!")){ return answer = n > m ? 1 : 0; }else if(operator.equals("<=")){ return answer = n <= m ? 1 : 0; }else return answer = n < m ? 1 : 0; } }
다른 방법 (인터넷 혹은 gpt 풀이)
class Solution { public int solution(String ineq, String eq, int n, int m) { return switch (ineq + eq) { case ">=" -> n >= m ? 1 : 0; case ">!" -> n > m ? 1 : 0; case "<=" -> n <= m ? 1 : 0; case "<!" -> n < m ? 1 : 0; default -> 0; }; } }
case → : Java 14이상에서 도입된 switch 표현식으로 기존의 switch 표현식과는 다르게 값을 return할 수 있다.
 
Share article

jay0628