Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类项中的构造函数项不能应用于给定类型;_Java_Bluej - Fatal编程技术网

Java 类项中的构造函数项不能应用于给定类型;

Java 类项中的构造函数项不能应用于给定类型;,java,bluej,Java,Bluej,所以我对Java非常陌生。。。已经做了大约4周了。。。温柔点 我试图让我的takeItem方法(如下)将itemName变量传递回我的Player类,以便我可以将当前房间中的项目添加到我的Player。我发现编译器错误:类项中的构造函数项不能应用于给定类型 我的最终目标是让player类在将对象从房间中移除后保留该对象 项目方法: private void takeItem(Command command) { if(!command.hasSecondWord()) {

所以我对Java非常陌生。。。已经做了大约4周了。。。温柔点

我试图让我的takeItem方法(如下)将itemName变量传递回我的Player类,以便我可以将当前房间中的项目添加到我的Player。我发现编译器错误:类项中的构造函数项不能应用于给定类型

我的最终目标是让player类在将对象从房间中移除后保留该对象

项目方法:

private void takeItem(Command command) 
{

    if(!command.hasSecondWord()) {
        // if there is no second word, we don't know where to go...
        System.out.println("Take what?");
        System.out.println();
        return;
    }

    String itemName = command.getSecondWord();
    Item theItem;
    // Try to take an item.
    theItem = new Item(player.getCurrentRoom().removeItem(itemName));


    if (theItem == null) 
    {
        System.out.println("There is no item!");
    }
    else 
    { 
        player.addItem(theItem);
        player.getItemsCarried();//print item info
    }
球员级别:

//above code omitted//
public void setCurrentRoom(Room room)
{
 currentRoom = room;
}

public Room getCurrentRoom()
{
 return currentRoom;
}

//code below omitted//


 public void addItem (Item thingy)
 {

     items.put(thingy.getName(), thingy);

 }

  //code below omitted//
项目类别:

public class Item
{
  // instance variables - replace the example below with your own
  private String name;
  private String description;
  private int weight;

   /**
  * Constructor for objects of class Item
  */
  public Item(String n, String d, int w)
  {
  name = n;
  description = d;
  weight = w;
  }
 //code below omitted//
房间等级:

public class Room 
{
private String description;
private HashMap <String, Room> exits;
private HashMap <String, Item> items;
//some code below omitted//


 public Room (String description) 
 {
    this.description = description;
    exits = new HashMap<>();
    items = new HashMap<>();
  }


public void addItem (Item thingy)
{
    items.put(thingy.getName(), thingy);
}

public String removeItem(String thingy)
{
    items.remove(thingy);
    return thingy;

}
//code below omitted
公共教室
{
私有字符串描述;
私有HashMap出口;
私有HashMap项;
//下面省略了一些代码//
公共房间(字符串描述)
{
this.description=描述;
exits=newhashmap();
items=newhashmap();
}
公共无效附加项(项目名称Y)
{
items.put(thingy.getName(),thingy);
}
公共字符串移除项(字符串类型)
{
项目。移除(东西);
归还物品;
}
//省略下面的代码

您在
类中的构造函数接受两个
字符串
参数和一个
int
,但是您试图通过只传入一个
字符串
来创建一个新的
项(无论
removietem()
方法返回什么)。您可以更改
removietem()
方法,使其返回已删除的
项,在这种情况下,您应该更改

itItem=新项目(player.getCurrentRoom().removietem(itemName));

itItem=player.getCurrentRoom().removeItem(itemName);


或者您可以使用必要的参数创建一个新的
项目

您的
项目
类在哪里,发布其构造函数/s。项目类:公共类项目{//实例变量-将下面的示例替换为您自己的私有字符串名称;私有字符串描述;私有int-weight;公共项(字符串n,字符串d,int-w){name=n;description=d;weight=w;}公共字符串getName(){return name;}公共字符串getDescription(){return description;}public int getWeight(){return weight;}}是否返回已删除的项目?如果是,则行应为
itItem=player.getCurrentRoom().removietem(itemName)
正如您当前所拥有的,您正在尝试创建一个新的
对象,在该对象中,您将
player.getCurrentRoom().removeItem(itemName)
返回的任何内容传递到构造函数中。@EyedJellyFish,如果我更改您建议的行,则为itItem=(player.getCurrentRoom().RemoveItemName));我遇到以下编译器错误:不兼容的类型:java.lang.String无法转换为Item@Evan向我们展示removeItem方法的代码。谢谢Jelly,我试过了,但是我得到的编译器错误是不兼容的类型:java.lang.String无法转换为Item@Evan同样,您需要更改removietem方法以返回字符串
分配给
项目
。谢谢!对不起,我是个傻瓜