0
下面的列表應該是我的程序的結果。雖然,#2我得到「荷馬打開門」。我不知道我是否正盯着錯誤。我一直在研究代碼一段時間,昏昏欲睡絕對沒有幫助。任何幫助表示讚賞!我的程序中有一個邏輯錯誤。任何人都可以發現什麼是錯的?
- // Bart把門鎖上。
- //荷馬試圖打開門,但不能因爲它被鎖定。
- //荷馬打開門。
- //荷馬打開大門。
- //巴特試圖打開門,但不能,因爲它已經打開。
- // Marge關上了門。
- //荷馬試圖關門,但不能,因爲它已經關閉了 。
- // Lisa打開門。
- //亞伯拉罕試圖鎖門,但不能因爲它的開放。
- // Marge關上了門。
- //亞伯拉罕鎖門。
- //巴特試圖打開門,但不能因爲它鎖定。
- // Lisa試圖打開門,但不能因爲鎖定。
- //荷馬試圖打開門,但不能因爲它被鎖定。
- //亞伯拉罕打開門。
- //荷馬打開大門。
// Marge關上了門。
//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; } } }
我的不好,我在java工作。正如我之前提到的IM有點累..反正那工作!謝謝 – Zach