Toplu Değiştirme
Değiştirmen gereken doğru kutuyu buldun. Yaşasın! Ne yazık ki, bir forEach döngüsüyle içeriği kolayca değiştiremezsin. Bunun için programını biraz değiştirip bir for döngüsü kullanıyorsun. Yeni çantanın ağırlığı 198.
Bu egzersiz
Orta Düzey Java
kursunun bir parçasıdırEgzersiz talimatları
- Yineleyici için doğru sınırı yaz.
weightslistesini değiştirmek için doğru indeksi gir. Öğenin konumu değişse bile çalışmalı.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
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);
}
}
}