我刚刚在三周前开始使用Java,一切都很顺利。虽然我的一个课程遇到了问题。在Fleet类中选择“new Fleet”并填写相应的参数字段时,我得到Error:incompatible types: java.lang.String cannot be converted to Ship.
我查看了各种类似问题的帖子和讨论,但没有找到任何帮助我的内容。
这是学校项目的作业,我非常感谢你理解它,不要直截了当地给我答案,只是指出我正确的方向。
提示: 问题陈述
美国海军委托您开发一套跟踪船队燃料消耗量的系统。每艘船都有一个名称(例如:“USS Montana”),燃料容量(船舶可以携带的最大燃料量),以及当前船上的燃料量。在这个问题中,燃料以“单位”来衡量,每艘船的容量是一个整数(例如:航空母舰的容量是125个燃料单位)。每个舰队中都有四艘船。部署舰队时,部署舰队中的每艘船。当部署一艘船时,它消耗了船上燃料的一半。当车队加油时,车队中的每艘船都加油。当船舶加油时,它完全被填满(其船上数量等于其容量)。
分配
创建一个名为Outlab2的BlueJ项目,添加一个名为Driver的类并将此代码粘贴到其中。不应修改驱动程序。 仔细查看驱动程序,因为它包含有关程序其余部分结构的线索。 您的Fleet类需要4种方法: 一个构造函数,它将4个Ships作为参数。 一种名为deploy的方法,用于部署车队中的每艘船。 一种称为加油的方法,将为船队中的每艘船加油。 一种名为printSummary的方法,它将为每艘船打印船舶名称以及船舶在其存在期间消耗的燃料单元总数。 通过查看驱动程序,您可以看到您还需要Ship类。该类的构造函数将以船名和燃料容量作为参数。 从问题陈述中推断出Ship类中需要的实例变量和方法。
这是我的代码: 船级
这是我尝试过的:
船级:
/**
* Ship's name, fuel levels, and fuel usage.
*
* @author JGW
* @version 11/20/15
*/
public class Ship
{
public String name;
public int fuelcapacity;
public int fuelcurrent;
public int fuelconsumed;
/**
* Constructor for objects of class Fleet
*/
public Ship(String n, int cap)
{
name = n;
fuelcapacity = cap;
fuelcurrent = fuelcapacity;
}
public void deploy()
{
fuelconsumed += fuelcurrent/2; //Adds the fuel used to the lifetime total consumption.
fuelcurrent = fuelcurrent/2; //Divides the current fuel in half.
}
public void reFuel()
{
fuelcurrent = fuelcapacity; //Refills the ship's fuel to maximum capacity.
}
public String getName()
{
return name;
}
public int getFuelconsumed()
{
int x = fuelconsumed; //Returns total fuel consumed.
return x;
}
Fleet class:
/**
* Calls upon methods for 4 different Ships as well as showing their lifetime fuel usage.
*
* @author JGW
* @version 11/20/15
*/
public class Fleet
{
public Ship ship1;
public Ship ship2;
public Ship ship3;
public Ship ship4;
/**
* Constructor for objects of class Fleet
*/
public Fleet(Ship s1, Ship s2, Ship s3, Ship s4)
{
ship1 = s1;
ship2 = s2;
ship3 = s3;
ship4 = s4;
}
public void deploy()
{
ship1.deploy(); //Deploys all 4 ships.
ship2.deploy();
ship3.deploy();
ship4.deploy();
}
public void reFuel()
{
ship1.reFuel(); //Refuels all 4 ships.
ship2.reFuel();
ship3.reFuel();
ship4.reFuel();
}
public void printSummary()
{
System.out.println(ship1.getName() + " " + ship1.getFuelconsumed());
System.out.println(ship2.getName() + " " + ship2.getFuelconsumed());
System.out.println(ship3.getName() + " " + ship3.getFuelconsumed());
System.out.println(ship4.getName() + " " + ship4.getFuelconsumed());
//Prints out how much fuel each ship has used.
}
}
驱动程序类:
/**
* Driver for Outlab2.
*/
public class Driver
{
public static void main(String[] args)
{
//Creating 4 instances of Ship
Ship ship1 = new Ship("Carrier", 150);
Ship ship2 = new Ship("Anti-Submarine", 35);
Ship ship3 = new Ship("Patrol", 22);
Ship ship4 = new Ship("Destroyer", 83);
//Creating instance of Fleet
Fleet fleet1 = new Fleet(ship1, ship2, ship3, ship4);
//Deploying the fleet twice
fleet1.deploy();
fleet1.deploy();
//Refuel the fleet once
fleet1.reFuel();
//Print summary
fleet1.printSummary();
}
}
答案 0 :(得分:1)
本声明:
When selecting "new Fleet" on the Fleet class
and filling in the appropriate parameter fields I get
Error:incompatible types: java.lang.String cannot be converted to Ship.
告诉我你将字符串传递给构造函数,如下所示:
Fleet fleet1 = new Fleet("ship1", "ship2", "ship3", "ship4");
但是这会失败,你在问题中传递的代码不会这样做。我怀疑你没有向我们展示你的完整代码。
仔细检查您所发布的内容,并验证您传递的变量确实是Ship,而不是String。
答案 1 :(得分:-1)
你的代码编译好了。确保您正在运行正确的文件,并保存所有内容。 eclipse中常见的初学者错误是选择错误的活动项目。
答案 2 :(得分:-2)
我没有阅读作业,但你的代码很好,没有任何错误,我只是遵守了它并且有效