프로그래머스 - 병과분류

윤민선·2026년 7월 28일

프로그래머스

목록 보기
55/73

문제 설명

환자의 증상 코드(code)가 주어진다.

증상 코드의 마지막 4글자를 확인하여 해당하는 진료과를 출력하는 문제이다.

마지막 4글자출력
_eyeOphthalmology
headNeurosurgery
inflOrthopedics
skinDermatology
그 외direct recommendation

접근 방법

문제에서 필요한 것은 문자열의 마지막 4글자이므로 substring() 메서드를 사용한다.

code.substring(code.length() - 4)

substring()으로 마지막 4글자를 추출한 뒤,

equals()를 사용하여 각 문자열과 비교한다.

조건에 맞는 진료과를 출력하고,

어느 조건에도 해당하지 않으면 "direct recommendation"을 출력한다.


풀이 순서

  1. Scanner를 이용하여 증상 코드를 입력받는다.
  2. substring()을 이용해 마지막 4글자를 추출한다.
  3. if-else문으로 마지막 4글자를 비교한다.
  4. 조건에 맞는 진료과를 출력한다.
  5. 해당하는 조건이 없으면 "direct recommendation"을 출력한다.

최종 코드

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String code = sc.next();
        String lastFourWords = code.substring(code.length() - 4);

        if (lastFourWords.equals("_eye")) {
            System.out.println("Ophthalmology");
        } else if (lastFourWords.equals("head")) {
            System.out.println("Neurosurgery");
        } else if (lastFourWords.equals("infl")) {
            System.out.println("Orthopedics");
        } else if (lastFourWords.equals("skin")) {
            System.out.println("Dermatology");
        } else {
            System.out.println("direct recommendation");
        }
    }
}

0개의 댓글