Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

프로그래머스: 서울에서 김서방 찾기 본문

Java

프로그래머스: 서울에서 김서방 찾기

Daeryuk Kim 2024. 3. 2. 18:21
import java.util.*;

class Solution {
    public String solution(String[] seoul) {
        String answer = "";
        int idx = -1;
        
        for (int i=0; i<seoul.length; i++) {
            if (seoul[i].equals("Kim")) {
                idx = i;
                break;
            }
        }
        
        if (idx != -1) {
            answer += "김서방은 " + idx + "에 있다";
        }
        
        return answer;
    }
    
    public static void main(String[] args) {
        String[] seoul = {"Jane", "Kim"};
        Solution sol = new Solution();
        String result = sol.solution(seoul);
        
        System.out.print(result); // 출력: "김서방은 1에 있다"
    }
}
public String findKim(String[] seoul){
        //x에 김서방의 위치를 저장하세요.
        int x = Arrays.asList(seoul).indexOf("Kim");        
        return "김서방은 "+ x + "에 있다";
    }

하지만 더 간략하게 한다면, 위와 같이 표현할 수 있다.