程序不会打印声明

时间:2016-05-21 14:41:40

标签: java methods

我有一个程序,我选择了一个添加船的选项,这会提示我给出一个例如b的ID。然后它会提示我输入容量。但是,我正在使用搜索方法来防止我可能第二次进入的任何重复ID。该程序编译,但我的声明“船舶ID已被使用”将不会打印出来。有什么想法吗?

这是我的代码。

public int search(String id)
{
    for(int index = 0; index < ships.length && ships[index] != null; ++index)
    {
        shipId = id;
        if (ships[index].getId().equals(id))
        {
            return index;
        }
    }
    //ship id not found so you can add ship
    return -1;
}

public void addShip( )
{
    System.out.println("Enter ship id >> ");
    String id = kb.nextLine();
    if(id.equals(search(id)))
    {
        System.out.println("Ship id already in use");
        return;
    }


    else
    {
        //for(int i = 0; i < ships.length; ++i)
        {
            System.out.println("Enter ship capacity");
            int capacity = kb.nextInt();
            ships[shipCounter++] = new Ship(id, capacity);
        }
    }
}

这是我的船级:

public class Ship
{
    private String id;
    private int capacity;
    private int currentCrew; // index into the Crew array
                             // points to the next free space
                             // in the Crew array
    private String status;

    private Crew [ ] crew;

    public Ship(String id, int capacity)
    {
        this.id = id;
        this.capacity = capacity;
        this.currentCrew = 0;
        crew = new Crew[capacity];

    }
    public Ship(String id, int capacity, String status)
    {
        this.id = id;
        this.capacity = capacity;
        this.status = "available";
        this.currentCrew = 0;
        crew = new Crew[capacity];

    }
    public void setId(String newId)
    {
        id = newId;
    }
    public void setCapacity(int newCapacity)
    {
        capacity = newCapacity;
    }
    public void setStatus(String newStatus)
    {
        if(status.equals("available"))
        {
            newStatus = "on station";
            status = newStatus;
        }
        else if(status.equals("on station"))
        {
            newStatus = "maintenance";
            status = newStatus;
        }
        else if(status.equals("station") || status.equals("maintenance"))
        {
            newStatus = "available";
            status = newStatus;
        }
        else
        {
            System.out.println("Invalid status");
        }
    }
    public String getId()
    {
        return id;
    }
    public String getStatus()
    {
        return status;
    }
    public int getCapacity()
    {
        return capacity;
    }
    public int getCurrentCrew()
    {
        return currentCrew;
    }
    public void addCrew()
    {
        //if(currentCrew < capacity)
        {
            //System.out.println("Enter crew id >> ");
            //String id = kb.nextLine();
        }
    }

    public String toString()
    {

        String sdesc = 
         '\n'
        + "Ship"
        + '\n'
        + "["
        + '\n'
        + " "
        + "Id: " + id
        + " capacity: " + capacity
        + " current crew: " + currentCrew
        + " status: " + status
        + '\n'
        + "]";

        return sdesc;
    }
}

2 个答案:

答案 0 :(得分:2)

您是否注意到这一行

 if(id.equals(search(id)))

id是String类型,但search返回类型是int。

如果您在Stringequals方法中看到

 if (anObject instanceof String) {

 }
 return false;

因此它只是假设

所以简单的解决方案是将int转换为String。

之类的东西
if(id.equals(search(id+"")))

答案 1 :(得分:0)

如果您想知道您是否已经拥有带有ID的货船,则应检查索引是否存在,这是您的搜索方法返回的内容。

 if(search(id) > 0)
 {
      System.out.println("Ship id already in use");
      return;
 }