การจำลองสถานการณ์
คุณกำลังพัฒนาการจำลองการชมบ้าน โดยสร้างวิธีโต้ตอบกับวัตถุต่าง ๆ ภายในการจำลองนี้
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Java ระดับกลาง
คำแนะนำการฝึกหัด
- เขียน keyword ที่ถูกต้องสำหรับการใช้
switch - ป้อน constant ที่เหมาะสมเพื่อให้
"Window"และ"Wall"มีพฤติกรรมเหมือนกัน - ใช้ keyword ที่เหมาะสมเพื่อให้โปรแกรมไม่ไปถึง
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;
}
}
}