환자의 증상 코드(code)가 주어진다.
증상 코드의 마지막 4글자를 확인하여 해당하는 진료과를 출력하는 문제이다.
| 마지막 4글자 | 출력 |
|---|---|
_eye | Ophthalmology |
head | Neurosurgery |
infl | Orthopedics |
skin | Dermatology |
| 그 외 | direct recommendation |
문제에서 필요한 것은 문자열의 마지막 4글자이므로 substring() 메서드를 사용한다.
code.substring(code.length() - 4)
substring()으로 마지막 4글자를 추출한 뒤,
equals()를 사용하여 각 문자열과 비교한다.
조건에 맞는 진료과를 출력하고,
어느 조건에도 해당하지 않으면 "direct recommendation"을 출력한다.
Scanner를 이용하여 증상 코드를 입력받는다.substring()을 이용해 마지막 4글자를 추출한다.if-else문으로 마지막 4글자를 비교한다."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");
}
}
}