1
當添加密鑰時,它顯示添加的密鑰,但是當我鍵入庫存時,它說揹包爲空,當我去使用該項目時,它不會讓我,因爲它不可用。項目顯示獲取密鑰,但不顯示在庫存中
public class Get extends AbstractCommand {
public Get (String name, String description){
super (name, description);
}
@Override
public void execute (Game game, String string) {
Item temp = game.getCurrentRoom().getItem(string);
UserInterface ui = game.getUI();
if(temp == null) ui.println("Item added\n");
else if (!(temp instanceof Moveable)) ui.println("item can not be moved");
else if (!game.getBackpack().add(temp)) ui.println("item can not be moved");
else game.getCurrentRoom().removeItem(temp);
}
}
@Override
public void execute(Game game, String string){
Item itemInBag = game.getBackpack().getItem(string);
Item itemInRoom = game.getCurrentRoom().getItem(string);
if(itemInBag!= null) {
if(itemInBag instanceof Useable){
game.setGameState(((Useable)itemInBag).use(game));
}else game.getUI().println("you can not use item");
}else if(itemInRoom!= null){
if(itemInRoom instanceof Useable){
game.setGameState(((Useable)itemInRoom).use(game));
}else game.getUI().println("this item can not be used");
}else {
game.getUI().println("take the item and use when you need to");
}
}
}
public class Inventory extends AbstractCommand {
public Inventory (String name, String description){
super (name, description);
}
@Override
public void execute (Game game, String string) {
if(((BackpackImpl)game.getBackpack()).getCount()==0) {
game.getUI().println("Backpack is empty");
}else {
for(Item i: game.getBackpack().getItems()) {
game.getUI().println(i.getName());
}
}
}
}
List<Item> items;
int maxCapacity;
public BackpackImpl (int maxCapacity){
this.maxCapacity = maxCapacity;
items = new ArrayList<Item>(5);
}
public BackpackImpl() {
this.maxCapacity = 5;
}
@Override
public boolean add(Item item){
if(items.size()>maxCapacity)
{System.out.println("your backpack canot fit no more\n");
return false;
}
items.add(item);
return true;
}
@Override
public Item remove(String string){
for(Item i: items){
if(((i.getName()).toLowerCase()).equals(string.toLowerCase())) {
items.remove(i);
return i;
}
}
return null;
}
@Override
public Item getItem(String string){
for(Item i: items){
if(((i.getName()).toLowerCase()).equals(string.toLowerCase())) {
return i;
}
}
return null;
}
@Override
public int getCapacity(){
return maxCapacity;
}
@Override
public List<Item> getItems(){
return items;
}
public int getCount(){
return items.size();
}
public void clear(){
items.clear();
}
}
這是夢幻般的它添加和刪除太感謝你了,但現在我使用的項目不起作用,很遺憾的是一個痛苦的我的項目的大學,它必須是在週二。任何建議:-) – user3211616
我已經整夜想弄明白了:-(你是一個救生員 – user3211616
要確認你的代碼做你認爲它做的,添加一個打印語句,如'System.out.println(「這裏「)'在每一步(這也是你經常使用'{}'的原因:你可以添加print語句而不改變其他任何東西)。添加變量到print語句看它們是否符合你的期望。基本上是手動調試:當你在調試模式下運行你的程序時,你的IDE應該能夠給你同樣的信息。 – vanOekel