如果语句无效? [重复]

问题描述 投票:-2回答:2

此问题已经在这里有了答案:

这听起来很愚蠢,我可以肯定我是愚蠢的,但是我不知道为什么它不起作用。

如果有疑问,是

if (currentRoom = outside || pub);
    {
        System.out.println("A friendly traveller is here!"); 

    }
}

下面是相关代码的其余部分

private Room currentRoom;

private void createRooms() 
{
    Room outside, theatre, pub, lab, office, up, down;

    // create the rooms
    outside = new Room("outside the main entrance of the university");
    theatre = new Room("in a lecture theatre");
    pub = new Room("in the campus pub");
    lab = new Room("in a computing lab");
    office = new Room("in the computing admin office");
    up = new Room("creepy upstairs");
    down = new Room("spooky downstairs");

    // initialise room exits
    outside.setExit("north", lab);
  //  outside.item.addItem("Gun", 10);
    outside.setExit("down", down);
    outside.setExit("up", up);
    outside.setExit("west", pub);
    outside.setExit("east", office);

    down.setExit("north", outside);
   // down.item.addItem("book", 1);
    up.setExit("south", outside);
    // up.item.addItem("knife", 5);

    lab.setExit("east", office);
    lab.setExit("south", outside);
    lab.setExit("north", theatre);
    // lab.item.addItem("bomb", 10);

    office.setExit("south", lab);
    // office.item.addItem("key", 0);

    pub.setExit("east", theatre);
    pub.setExit("south", outside);
    // pub.item.addItem("beer", 2);

    theatre.setExit("south", outside);
    currentRoom = outside;  // start game outside
}

    private void printWelcome() 
{
    System.out.println();
    System.out.println("Welcome to the World of Zuul!");
    System.out.println("World of Zuul is a new, incredibly boring adventure game.");
    System.out.println("Type 'help' if you need help.");
    System.out.println();
    System.out.println(currentRoom.getLongDescription());

    if (currentRoom = outside || pub);
    {
        System.out.println("A friendly traveller is here!"); 

    }
}

im实际上只是试图使它成为可能,如果玩家房间位于室外或酒吧,则可以打印一条消息。但是它不起作用,任何指导都非常感谢。我确定这是一个非常愚蠢的问题,但我是新来的,我不明白为什么它不起作用

java if-statement
2个回答
0
投票
if ((currentRoom == outside) || (currentRoom ==pub))

if (currentRoom == outside || currentRoom ==pub)

我想我应该再添加一些,但是我不确定是什么。用通用语言来说,在“或”上存在比较的隐式“分布”,就像乘法在加法上分布一样。

以同样的方式,我们可以将3 *(1 + 2)扩展为3 * 1 + 3 * 2,当我们说“ x等于3或4”时,我们在思维上“扩展”这个“ x等于3或x等于4“。后一种形式是用来编写大多数(全部?)计算机语言以正确解析的形式。


0
投票

三个错误

  • [=应该是==
  • [currentRoom = outside || pub应该是currentRoom = outside || currentRoom == pub
  • [if(...); {...}应为if(...) {...}(删除分号)] >>
© www.soinside.com 2019 - 2024. All rights reserved.