開始使用免費開始

奇數加總

你想知道奇數的總和累加到 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);
  }
}
編輯並執行程式碼