0

最近 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;
}
}

これは私の 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;
}
}
4

1 に答える 1