一括変更
変更すべき正しい箱が見つかりました。やりました! ただし、forEach ループでは内容を簡単に変更できません。そこで、プログラムを少し変更して for ループを使います。新しいバッグの重さは 198 です。
この演習はコースの一部です
中級 Java
演習の手順
- 反復子の正しい 上限 を書いてください。
- リスト
weightsを変更するための正しい インデックス を入力してください。要素の位置が変わっても動作するようにしましょう。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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);
}
}
}