console-calculator/src/main/java/org/example/lv2 at main · BibliyaSeo/console-calculator
기존 코드
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> 📌
조건을 (index < 0 || index <= resultList.size()-1) 에서 (index >= 0 && index < resultList.size())로 변경
값 수정 부분을 왜 밖에 뒀었는지! index 조건이 맞을 때 진행하는 거로 변경.
break; 추가 </aside>
기존 코드