0

下面的列表应该是我的程序的结果。不过,对于#2,我得到“荷马开门”。我不知道我是否正盯着错误。我已经研究了一段时间的代码并且困了绝对没有帮助。任何帮助表示赞赏!

  1. //巴特锁门。
  2. //荷马试图打开门,但不能因为它锁着。
  3. //荷马打开门。
  4. //荷马开门。
  5. //Bart 试图打开门,但不能,因为它已经打开了。
  6. //玛吉关上门。
  7. //Homer 试图关门,但不能,因为它已经关上了。
  8. //丽莎开门。
  9. //亚伯拉罕试图锁门,但不能,因为它开着。
  10. //玛吉关上门。
  11. //亚伯拉罕锁上了门。
  12. //Bart 试图打开门,但不能因为它锁着。
  13. //丽莎试图打开门,但不能因为它锁着。
  14. //荷马试图打开门,但不能因为它锁着。
  15. //亚伯拉罕打开门。
  16. //荷马开门。
  17. //玛吉关上门。

    //in a separate method:
    
    bart.lockDoor();
    homer.openDoor();
    homer.unlockDoor();
    homer.openDoor();
    bart.openDoor();
    marge.closeDoor();
    homer.closeDoor();
    lisa.openDoor();
    abraham.unlockDoor();
    marge.closeDoor();
    abraham.lockDoor();
    bart.openDoor();
    lisa.openDoor();
    homer.openDoor();
    abraham.unlockDoor();
    homer.openDoor();
    marge.closeDoor();
    
     boolean locked;
     boolean open;
    
     public void lockDoor()
    {
        if(locked == true)
            out.println( name + " tries to lock the door, but can't because its already locked.");
            else
                if(open == false)
                {
                    locked = true;
                    out.println( name + " locks the door.");
    
                }
    }
    
    public void unlockDoor()
    {
        if(open == true)
            out.println( name + " tries to unlock the door, but can't because its open.");
                else
                    if(locked == false)
                        out.println( name + " tries to unlock the door, but can't because its already unlocked.");
                        else
                            if(locked == true)
                            {
                                out.println( name + " unlocks the door.");
                                locked = false;
                            }
    
    }
    
    public void openDoor()
    {
        if(open == true)
            out.println( name + " tries to open the door, but can't because its already open.");
            else
                if( locked == true )
                    out.println( name + " tries to open the door, but can't because its locked.");
                    else
                        if(locked == false)
                        {
                            out.println( name + " opened the door.");
                            open = true;
                        }
    
    }
    
    public void closeDoor()
    {
        if(open == false)
            out.println( name + " tries to close the door, but can't because its already closed.");
            else
            {
                if(open == true)
                {
                    out.println( name + " closed the door.");
                    open = false;
                }
    
            }
    }
    
4

1 回答 1

1

您没有指定语言或完整的程序,但看起来问题在于每个人(有效地)都有自己的门,独立于彼此的门,因为每个人都有自己的锁定/打开标志来跟踪他们的门.

如果您希望只有一扇门,您需要让所有人共享一个锁定和单个打开的标志。根据您使用的语言,您可以通过制作 flags 来做到这一点static。或者,您可以将它们放在单独的“门”对象中,并让所有“人”对象引用该“门”

于 2013-01-12T00:40:02.680 回答