始める無料で始める

シミュレーション

住宅見学のシミュレーションを実装しています。そのために、シミュレーション内のさまざまなオブジェクトとやり取りする方法を作成します。

この演習はコースの一部です

中級 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;
    }
  }
}
コードを編集して実行