เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

รายงานสภาพอากาศ

คุณค้นพบวิธีดึงข้อมูลรายงานสภาพอากาศในพื้นที่ของคุณแล้ว และต้องการสร้างโปรแกรมเล็ก ๆ ที่บอกว่าควรใช้สิ่งของอะไรในสภาพอากาศแต่ละแบบ

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

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");
    }
  }
}
แก้ไขและรันโค้ด