참고 코드


console-calculator/src/main/java/org/example/lv2 at main · BibliyaSeo/console-calculator

문제 사항 및 수정 사항


  1. 인덱스값을 큰거를 입력하고 원하는 값을 입력하면 오류남. 분명 다시 입력했는데도 처음 입력한index의 값이 변하지 않고 그대로 들어가는 것 같음. 새로운 값이 적용되게 해야 함.

기존 코드

switch (answer) {
    case "update":
        while (true) {
            System.out.print("수정할 값의 인덱스를 입력해 주세요: ");
            int index = 0;
            if (scanner.hasNextInt()) {
                index = scanner.nextInt();
                if (index < 0 || index <= resultList.size()-1) {
										System.out.print("현재 입력할 수 있는 값은 0~" + (resultList.size() - 1) + "까의 정수입니다. 다시 입력해 주세요: ");                 
                }
            } else {
                System.out.print("정수를 입력해 주세요.");
                scanner.next();
            }
            
            // 값 수정
						int updateResult = getPositiveInt(scanner, "변경을 원하시는");
						calc.updateResultList(index, updateResult);
						break;
        }
    default:
}

코드 수정

switch (answer) {
    case "update":
        while (true) {
            System.out.print("수정할 값의 인덱스를 입력해 주세요: ");
            int index = 0;
            if (scanner.hasNextInt()) {
                index = scanner.nextInt();
                if (index >= 0 && index < resultList.size()) {
                    // Todo: 인덱스값을 큰거를 입력하고 원하는 값을 입력하면 오류남
                    // 처음 입력한index의 값이 변하지 않고 그대로 들어가는 게 아닌지 확인 필요!
                    // 새로운 값을 적용하게 해야 함

                    // 값 수정
                    int updateResult = getPositiveInt(scanner, "변경을 원하시는");
                    calc.updateResultList(index, updateResult);
                    break;
                } else {
                    System.out.print("현재 입력할 수 있는 값은 0~" + (resultList.size() - 1) + "까의 정수입니다. 다시 입력해 주세요: ");
                }
            } else {
                System.out.print("정수를 입력해 주세요.");
                scanner.next();
            }
        }
    default:
}

<aside> 📌

  1. 조건을 (index < 0 || index <= resultList.size()-1) 에서 (index >= 0 && index < resultList.size())로 변경

  2. 값 수정 부분을 왜 밖에 뒀었는지! index 조건이 맞을 때 진행하는 거로 변경.

  3. break; 추가 </aside>


  1. 기존에 exit if로 관리해 줬지만 lv2에서 remove, update가 추가되면서 조건이 많아져서 switch 문으로 관리해줌

기존 코드