開始使用免費開始

模擬

你正在實作一個看屋模擬。為了達成這個目標,你要建立與模擬中不同物件互動的方式。

本練習屬於課程

Java 中級

檢視課程

練習說明

  • 寫出用來進行 switch 的正確關鍵字
  • 輸入正確的常數,讓 "Window""Wall" 具有相同行為。
  • 使用正確的關鍵字,確保不會通過 default

動手互動練習

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

class Interactor {
  public static void main(String[] args) {
    String object = "Wall";

    // Write down the correct keyword to allow for case switching
    ____ (object) {
      case "Door":
        System.out.println("You have discovered a new room");
        break;
      case "Fridge":
        System.out.println("The food is all yours");
        break;
      // Enter the correct constant to give the same behavior to Window and Wall
      case "____":
      case "Window":
        System.out.println("I can't go through that");
        // Make sure you don't go through the default case by using the correct keyword
        ____;
      default:
        System.out.println("This object cannot be interacted with");
        break;
    }
  }
}
編輯並執行程式碼