일괄 변경
바꿔야 할 올바른 상자를 찾았어요. 잘하셨어요! 아쉽게도 forEach 루프만으로는 내용을 쉽게 바꾸기 어렵습니다. 이를 위해 프로그램을 조금 수정해서 for 루프를 사용해 보세요. 새 가방의 무게는 198입니다.
이 연습은 강의의 일부입니다
중급 Java
연습 안내
- 반복자의 올바른 limit를 작성하세요.
- 목록
weights를 변경하기 위한 올바른 index를 입력하세요. 요소의 위치가 바뀌어도 동작해야 합니다.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class WeightChanger {
public static void main(String[] args) {
int[] weights = {198, 190, 188, 187, 190, 198, 201, 250, 203, 210, 205, 170, 180, 200, 203, 210, 180};
// Write down the correct limit for the iterator
for (int i = 0; i < ____.____; i++) {
int weight = weights[i];
if (weight > 220) {
// Input the correct index to change the list. It should work even if the element changed location.
weights[____] = 198;
}
}
for (int weight : weights){
System.out.println(weight);
}
}
}