Java 向文件室添加项目,未定义类型错误
编辑:如果我不清楚的话(对不起,我是Java的初学者),我只是想增加将项目添加到房间中的可能性,但是,我希望项目方面在它自己的类中,就像现在一样。虽然在目前的状态下,它不起作用 我试图让物品出现在我游戏的房间里,所以我创建了一个单独的物品类,相关代码如下所示Java 向文件室添加项目,未定义类型错误,java,Java,编辑:如果我不清楚的话(对不起,我是Java的初学者),我只是想增加将项目添加到房间中的可能性,但是,我希望项目方面在它自己的类中,就像现在一样。虽然在目前的状态下,它不起作用 我试图让物品出现在我游戏的房间里,所以我创建了一个单独的物品类,相关代码如下所示 public class Items { private String itemDescription; private int itemWeight; /** * Add an item to the Room * @param
public class Items
{
private String itemDescription;
private int itemWeight;
/**
* Add an item to the Room
* @param description The description of the item
* @param weight The item's weight
*/
public void addItem(String description, int weight)
{
itemDescription = description;
itemWeight = weight;
}
/**
* Does the room contain an item
* @param description the item
* @ return the item's weight or 0 if none
*/
public int containsItem(String description)
{
if (itemDescription.equals(description))
{
return itemWeight;
}
else
{
return 0;
}
}
/**
* Remove an item from the Room
*/
public String removeItem(String description)
{
if (itemDescription.equals(description))
{
String tmp = itemDescription;
itemDescription = null;
return tmp;
}
else
{
System.out.println("This room does not contain" + description);
return null;
}
}
public String getItemDescription()
{
return this.itemDescription;
}
public void setItemDescription(String itemDescription)
{
this.itemDescription = itemDescription;
}
public int getItemWeight()
{
return this.itemWeight;
}
public void setItemWeight(int itemWeight)
{
this.itemWeight = itemWeight;
}
public String getCharacter()
{
return this.character;
}
public void setCharacter(String character)
{
this.character = character;
}
}
在我单独的游戏类中,我尝试将它链接到我的游戏类中,就像这样
// initialise room exits
outside.setExit("north", lab);
outside.addItem("notebook", 2);
就像我在比赛的出口一样。但是,我收到一条错误消息
The method addItem(String, int) is undefined for the type
Room
这是房间教室
import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;
public class Room
{
private String description;
private HashMap<String, Room> exits;
/*
private ArrayList<Item> items;
*/
//private HashMap<String, Item> items;
public Items item;
// Characters in the room
private String character;
/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
* "an open court yard".
* @param description The room's description.
*/
public Room(String description)
{
this.description = description;
exits = new HashMap<String, Room>();
}
/**
* Define the exits of this room. Every direction either leads
* to another room or is null (no exit there).
* @param north The north exit.
* @param east The east east.
* @param south The south exit.
* @param west The west exit.
* @param up The upwards exit.
* @param down The downwards exit.
*
*/
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room in the given direction.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
public Room getExit(String direction)
{
return exits.get(direction);
}
/**
* Return a description of the room’s exits,
* for example "Exits: north west".
* @return A description of the available exits.
*/
public String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys)
{
returnString += " " + exit;
}
return returnString;
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
/**
* Return a long description of this room, of the form:
* You are in the kitchen.
* Exits: north west
* @return A description of the room, including exits.
*/
public String getLongDescription()
{
return "You are " + description + ".\n" + getExitString();
}
}
我想这是因为,这个叫做“房间”的课程没有提到“物品”,但是我不知道如何才能做到这一点?任何指导都将不胜感激。您可以在这里混合项目逻辑和房间逻辑
addItem
函数应该是项的构造函数containsItem
方法也应位于Room类中import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;
public class Room
{
public Items item = new Items();
...........
在调用类中,像这样使用它
outside.item.addIem(...)
你是如何定义教室的?方法
addItem
在类项中定义,是否也在房间中定义?发布两个类的完整代码。@girishbhi!谢谢你的回复,我刚刚为这两个类添加了完整的代码。在Room类中没有addItem方法。但你却在叫它,那是你的error@Stultuske我在另一个评论中说:对不起,我对Java真的很陌生,你的意思是简单地使用我的items类(public void addItem)中的内容,还是只是引用它的一种方式?我真的很困惑如何做后一个你不能添加一个“引用”到一个方法中的一个方法。您需要在Room类中定义public void addItem(String name,int number){},因为这是您调用它的地方。否则,您将需要更改该调用,但由于我们不知道您正在尝试做什么,因此我们建议您最好添加该方法1。所以Items Item=新项??2/3. 我相信我确实在房间里设置了一个数组列表,我刚刚发布的更新帖子中的代码被删除了,但由于它不起作用,我对Java真的很陌生,你的意思是简单地获取我的items类(public void addItem)中的内容,还是只是引用它的一种方式?亲爱的亚当,每个人在某个时候都是初学者。我相信你能做到。我试图在上面的回答中添加更多的上下文。看看是否有帮助。祝你好运谢谢你的好意,在阅读了你的建议后,我想我已经注意到了一个错误,我在游戏类中声明了新的房间,我没有在我的帖子中指定,但是我现在将添加代码。在那门课上,我已经有了“室外=新房间(“主入口外”)等等。所以你的上述帮助在room类中不起作用我不认为?我添加了我所指的代码可能是我在Toom类中遇到了你的问题…私有字符串项;为什么它是字符串类型?它不应该是公共项项吗?检查更新的回答抱歉重复评论,错误现在已经更改,而不是方法addItem(String,int)对于类型Room未定义…现在相同,但对于类型“Items”而不是Room
import java.util.HashMap;
import java.util.Set;
import java.util.ArrayList;
public class Room
{
public Items item = new Items();
...........
outside.item.addIem(...)