开始使用免费开始使用

模拟

您正在实现一个看房模拟。为此,您要创建与模拟中不同对象交互的方式。

本练习是课程的一部分

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;
    }
  }
}
编辑并运行代码