開始使用免費開始

天氣小報童

你已經找到方法,能取得你所在地的天氣報告。你想做一個小程式,根據不同的天氣告訴你該使用哪一樣物品。

本練習屬於課程

Java 中級

檢視課程

練習說明

  • 使用正確的關鍵字,讓它成為可檢查的 case
  • 放入正確的常數,只有在 "Sunny" 時才會印出。
  • 寫上正確的關鍵字,確保只有一個 println() 會生效。

動手互動練習

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

class WeatherReporter {
  public static void main(String[] args) {
    String weather = "Sunny";

    switch (weather) {
      // Use the correct keyword to make it a case to check
      ____ "Windy":
        System.out.println("Take an umbrella");
        break;
      // Enter the appropriate name to check for sunny
      case "____":
        System.out.println("Don't forget your sunscreen");
        // Write the correct keyword down to make sure the message will be unique
        ____;
      case "Cloudy":
        System.out.println("The umbrella might be great");
        break;
      default:
        System.out.println("I don't know what the weather will be like");
    }
  }
}
編輯並執行程式碼