开始使用免费开始使用

奇数求和

您想了解奇数求和在不超过 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);
  }
}
编辑并运行代码