我最近更改了我的 Locale 构造函数以接收一个名为 Item 的数组,而不仅仅是一个项目名称,现在我在我的游戏中获取一个项目时遇到了一些困难。这是一款文字冒险游戏,玩家可以在地图上移动并拾取房间内的物品。我收到错误消息:“ArrayList 类型中的方法 add(String) 不适用于参数 (Item)”。我不确定如何解决这个问题。
谢谢大家的帮助!
这是我的代码:
static int currentLocation = player1.currentRoom;
//Items {itemName, itemDes}
static Item[] items = {
new Item ("map","A layout of your house."),
new Item ("battery", "A double A battery."),
new Item ("flashlight", "A small silver flashlight."),
new Item ("key", "This unlocks some door in your house."),
};
//Locations {roomName, description, Item}
static Locale[] locales = {
new Locale("bedroom","You see the outline of a bed with your childhood stuffed bear on it.",items[0]),
new Locale("hallway","A carpeted floor and long pictured walls lie ahead of you.",null),
new Locale("kitchen","The shining surface of your stove reflects the pale moonlight coming in the window over the sink.",items[1]),
new Locale("bathroom","You find yourself standing in front of a mirror, looking back at yourself.",items[2]),
new Locale("living room","You stub your toe on the sofa in the room, almost falling right into the TV.",null),
new Locale("dining room","You bump the china cabinet which holds your expensive dishes and silverware.",items[3]),
new Locale("office","The blinking light from the monitor on your desk can be seen in the dark",null),
new Locale("library","The smell of old books surrounds you.",null),
new Locale("basement","You reach the top of some stairs and upon descending down, you find the large metal generator.",null),
};
//Take
else if(response.equalsIgnoreCase("T")){
Locale locale = locales[currentLocation];
// check if the locale has an item.
if(locale.item != null){
// create an "ArrayList" with all the items from the players inventory or an empty one if the inventory is "null"
ArrayList<String> inventory;
if(player1.inventory != null){
inventory = new ArrayList<String>(Arrays.asList(player1.inventory));
}
else{
inventory = new ArrayList();
}
// add the item to the list and set the list as the player's inventory by converting it to an array.
inventory.add(locale.item);
player1.inventory = inventory.toArray(new String[inventory.size()]);
System.out.println("\tA " + locale.item + " was added to the inventory");
System.out.println("\n\tYou can view your inventory by pressing 'I'.");
System.out.println("\n\tFive points have also been added to your score.");
player1.score += 5;
System.out.println("\n\tThis is your current score: "+player1.score);
locale.item = null;
}
// this locale doesn't have an item.
else{
System.out.println("\tThere is no item to pick up");
}
break;
}//End of Take
这是我的语言环境类:
public class Locale {
//Locale must have name, description, and Item
public static int roomNumber;
public String roomName;
public String description;
public Item item;
public Locale(String roomName, String description, Item item){
this.roomName = roomName;
this.description = description;
this.item = Item;
}
}
这是我的项目类:
public class Item {
//item must have a name and a description (both strings)
public String itemName;
public String itemDes;
public Item (String itemName, String itemDes){
this.itemName = itemName;
this.itemDes = itemDes;
}
}