코드 알고리즘
[PCCE 기출문제] 4번 / 병과분류
수바니
2025. 1. 2. 11:23
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, code.length());
if(lastFourWords.equals()){
System.out.println("Ophthalmologyc");
}
else if(){
System.out.println("Neurosurgery");
}
else if(){
System.out.println("Orthopedics");
}
{
System.out.println("Dermatology");
}
{
System.out.println("direct recommendation");
}
}
}
String lastFourWords = code.substring(code.length()-4, code.length());
1. code.length() : 문자열의 전체 길이 반환
2. code.length() -4 : 문자열의 길이에서 4를 뺀값을 계산
Ex) "HelloWorld"라면 code.length()-4 == 6
3. code.substring(startIndex, endIndex) :
code 문자열의 시작인덱스부터 마지막인덱스 직전까지의 부분 문자열을 추출
code.substring(code.length()-4, code.length()) 에서의 시작인덱스는 code.length()-4, 마지막인덱스는 code.length()
Ex) "HelloWorld".substring(6, 10) 은 "World"
따라서 lastFourWords는 code의 마지막 네 문자를 포함하는 문자열이 되는것이다.