BaşlayınÜcretsiz başlayın

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, kursun bir parçasıdır

Orta Düzey Java

Kursa Göz Atın

Egzersiz talimatları

  • Yineleyici için doğru sınırı yaz.
  • weights listesini değiştirmek için doğru indeksi gir. Öğenin konumu değişse bile çalışmalı.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

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);
    }
  }
}
Kodu Düzenle ve Çalıştır