Aan de slagGa gratis aan de slag

Batch Changing

You have found the correct box to change. Hurray ! Unfortunately, with a forEach loop, you cannot easily change the content. To that end, you slightly change your program and use a for loop. The weight of the new bag is 198.

Deze oefening maakt deel uit van de cursus

Intermediate Java

Cursus bekijken

Oefeninstructies

  • Write down the correct limit for the iterator.
  • Input the correct index to change the list weights. It should work even if the element's position changes.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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);
    }
  }
}
Code bewerken en uitvoeren