【发布时间】:2014-11-28 17:53:18
【问题描述】:
public void drop (String name) - 如果合适,从 ArrayList 中删除该项目并将其添加到当前房间。使用以下选项之一更新游戏消息:1) 玩家没有持有该物品,2) 房间已有物品,或 3) 玩家已成功将物品放入房间。这是此方法的目标,但是当我运行它时,它总是跳到 else 语句中的 currentMessage。
问题: 我遇到的问题是,当我运行此方法并尝试将项目放入房间时,它不会跳到 else 语句并返回消息“您没有该项目”,我不知道为什么正在这样做并且没有通过第一个 if 语句,因为我正在输入一个我知道在 arraylist 中的项目名称。
public void drop(String name)
{
for(Item count : myArray){
if(count.getName().contains(name) && currentRoom.hasItem() == false){
currentRoom.addItem(count);
currentMessage = "you have successfully dropped the item in the room";
myArray.remove(count);
}
else if(count.getName().contains(name) && currentRoom.hasItem() == true)
{
currentMessage = "the room already has an item";
}
else
{
currentMessage = "you do not have that item";
}
}
}
【问题讨论】:
-
那么究竟是什么问题?