始める無料で始める

奇数の合計

100 までの奇数の合計がどれくらいの速さで増えるかを知りたいですね。その処理を行うコードを書いてみましょう。

この演習はコースの一部です

中級 Java

コースを見る

演習の手順

  • ループを作成するための正しいキーワードを入力します。
  • 到達した total を保持する適切な変数を呼び出します。
  • 毎回の実行で i が 2 ずつ増えるよう、適切な演算を使います。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

class Adder {
  public static void main(String[] args) {
    int total = 0;
    int i = 1;

    // Enter the correct keyword to create the loop
    ____ (total < 100) {
      // Call the appropriate variable to store the total reached
      ____ += i;
      // Use the fitting operation to have i increase by 2 each run
      i ____ 2;
    }

    System.out.println("We reach " + total + " when summing up to " + i);
  }
}
コードを編集して実行