下面的列表应该是我的程序的结果。不过,对于#2,我得到“荷马开门”。我不知道我是否正盯着错误。我已经研究了一段时间的代码并且困了绝对没有帮助。任何帮助表示赞赏!
- //巴特锁门。
- //荷马试图打开门,但不能因为它锁着。
- //荷马打开门。
- //荷马开门。
- //Bart 试图打开门,但不能,因为它已经打开了。
- //玛吉关上门。
- //Homer 试图关门,但不能,因为它已经关上了。
- //丽莎开门。
- //亚伯拉罕试图锁门,但不能,因为它开着。
- //玛吉关上门。
- //亚伯拉罕锁上了门。
- //Bart 试图打开门,但不能因为它锁着。
- //丽莎试图打开门,但不能因为它锁着。
- //荷马试图打开门,但不能因为它锁着。
- //亚伯拉罕打开门。
- //荷马开门。
//玛吉关上门。
//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; } } }