언어/JAVA

자료구조별 요리 레시피 출력

nakgopsae 2024. 7. 11. 12:43

요구사항



코드

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String collectionName = sc.nextLine();
        String title = sc.nextLine();

        switch (collectionName){
            case "list":
                ArrayList<String> strList = new ArrayList<>();
                while(true){
                    String text = sc.nextLine();
                    if(text.equals("끝")){
                        break;
                    }
                    strList.add(text);

                }
                title = collectionName+"+"+"["+ title + "]";
                System.out.println(title);
                for (int i = 0; i < strList.size(); i++) {
                    int num = i+1;
                    System.out.println(num + ". " + strList.get(i));
                }
                break;
            case "map":
                Map<Integer,String> strMap = new HashMap<>();
                int lineNum = 1;
                while(true){
                    String text = sc.nextLine();
                    if(text.equals("끝")){
                        break;
                    }
                    strMap.put(lineNum++ ,text);

                }
                title = collectionName+"+"+"["+ title + "]";
                System.out.println(title);
                for (int i = 0; i < strMap.size(); i++) {
                    int num = i+1;
                    System.out.println(num + ". " + strMap.get(i+1));
                }
                break;
            case "set":
                Set<String> strSet = new HashSet<>();
                while(true){
                    String text = sc.nextLine();
                    if(text.equals("끝")){
                        break;
                    }
                    strSet.add(text);

                }
                title = collectionName+"+"+"["+ title + "]";
                System.out.println(title);
                Iterator<String> iterator = strSet.iterator();
                for (int i = 0; i < strSet.size(); i++) {
                    int num = i+1;
                    System.out.println(num + ". " + iterator.next());
                }
                break;
            default:
                System.out.println("저장할 수 없는 자료구조입니다.");
        }
    }

 

 

요구사항을 이해하는데 오래걸렸다 흠흠 

해설영상보고 감잡았음

Set을 처음 써봐서 좀 애먹었다 

분발하자

'언어 > JAVA' 카테고리의 다른 글

if문 가위 바위 보  (0) 2024.07.10
입출력  (0) 2024.07.09
wrapper class / 변수 +  (0) 2024.07.09
JDK 설치  (0) 2024.07.09
JVM  (0) 2024.07.08