開始使用免費開始

跳出迴圈

為了示範,這裡提供一個在程式碼中使用 break 的例子。不過,真實情境的範例通常較為複雜,難以放進一個練習裡。

本練習屬於課程

Java 中級

檢視課程

練習說明

  • 使用正確的關鍵字來停止迴圈。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

class Breaker {
  public static void main(String[] args) {
    for (int i = 1; i < 64; i*=2) {
      System.out.println(i);
      if (i >= 8) {
        // Use the proper keyword to stop the loop
        ____;
      }
    }
    System.out.println("Broke out of the loop");
  }
}
編輯並執行程式碼