缺少退货声明错误基本程序

时间:2016-02-06 19:50:29

标签: java

我正在为学校的一个班级写一个小程序,并且遇到了错误的返回语句错误。"我试过谷歌搜索错误,看看我是否能找到一个可能对我有帮助的通用答案,但我对我的代码所做的一切都没有能够摆脱这个错误,以便我的程序可以编译。作为参考,这是我试图编写的程序的分配:

美国海军委托您开发一套跟踪船队燃料消耗量的系统。每艘船都有一个名称(例如:" USS Montana"),燃料容量(船舶可以携带的最大燃料量),以及当前船上的燃料量。在这个问题中,燃料是在"单位"每艘船的容量是一个整数(例如:承运人的容量是125个燃料单位)。每个舰队中都有四艘船。部署舰队时,部署舰队中的每艘船。当部署一艘船时,它消耗了船上燃料的一半。当车队加油时,车队中的每艘船都加油。当船舶加油时,它完全被填满(其船上数量等于其容量)

这是我的代码:

/**
 * Navy Ship Descriptions
 * 
 * @author Elizabeth Rehbein
 * @version 01/28/16
 */
public class Ship
{

    // instance variables 
    private String name;        //ship name
    private double fuelCapacity;       //ship fuel capacity
    private double fuelCurrent;  //the ship's current fuel on ship after deployments
    private double fuelConsumed;   //the amount of fuel a ship has consumed after deployments


    //constructor
    public Ship(String startName, double startFuelCapacity, double startFuelCurrent, double startFuelConsumed)
    {
        // initialise instance variables
        name=startName;
        fuelCapacity=startFuelCapacity;
        fuelCurrent=startFuelCurrent;
        fuelConsumed=startFuelConsumed;
    }

   //get methods
    public String getName()
    {
        return name;    //returns the name of the ship being called
    }

    public double getFuelCapacity()
    {
        return fuelCapacity;    //the ship's fuel tank capacity
    }

    public double getFuelCurrent()
    {
        return fuelCurrent; //returns the fuel a ship currently has before being refilled
    }

    public double getFuelConsumed()
    {
        return fuelConsumed;               //returns the fuel consumed by a ship
    }

    public void deploy()
    {
        fuelCurrent = fuelCurrent/2;     //reduces ships current fuel by half with every depolyment
        fuelConsumed += fuelCapacity/2;  //adds the fuel used in depolyment to fuelConsumed to track total fuel used
    }

    public double reFuel()
    //refueling each ship returns it to its full fuel capacity
    {
        fuelCurrent = fuelCapacity;
    }   
}




/**
 * A fleet of Navy ships.
 * 
 * @author Elizabeth Rehbein 
 * @version 02/05/16
 */
public class Fleet
{
    // instance variables 
    private Ship ship1;     //ship1 in the fleet
    private Ship ship2;     //ship2 in the fleet
    private Ship ship3;     //ship3 in the fleet
    private Ship ship4;     //ship4 in the fleet

    //constructor
    public Fleet(Ship inShip1, Ship inShip2, Ship inShip3, Ship inShip4)
    {
        ship1=inShip1;
        ship2=inShip2;
        ship3=inShip3;
        ship4=inShip4;
    }


    //deploys the ships
    public void deploy()
    {
        ship1.deploy();
        ship2.deploy();
        ship3.deploy();
        ship4.deploy();
    }

    //refuels the ships after deployment
    public void reFuel()
    {
        ship1.reFuel();
        ship2.reFuel();
        ship3.reFuel();
        ship4.reFuel();
    }

    //prints out the fuel consumed for each ship in the fleet
    public double printSummary()
    //prints out fuelConsumed for each ship
    {
    System.out.println(ship1.getName() +" 's total fuel consumption is " + ship1.getFuelConsumed());
    System.out.println(ship2.getName() +" 's total fuel consumption is " + ship2.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship3.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship4.getFuelConsumed());
   }
}

/**
 * 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();
    }
}

此外,我不应该更改此作业的驱动程序。

让我知道你对我可能出错的地方的看法。 干杯!

4 个答案:

答案 0 :(得分:3)

这里:

public double reFuel()
    //refueling each ship returns it to its full fuel capacity
    {
        fuelCurrent = fuelCapacity;
    }

在这里:

//prints out the fuel consumed for each ship in the fleet
    public double printSummary()
    //prints out fuelConsumed for each ship
    {
    System.out.println(ship1.getName() +" 's total fuel consumption is " + ship1.getFuelConsumed());
    System.out.println(ship2.getName() +" 's total fuel consumption is " + ship2.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship3.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship4.getFuelConsumed());
   }

你有两种方法,你已经说过它们会返回double,但在它们的身体里却没有return A_DOUBE。通过添加return statement来修复它们,或者只是根据您的问题制作它们void

答案 1 :(得分:2)

这里缺少

return声明:

public double reFuel()
//refueling each ship returns it to its full fuel capacity
{
    fuelCurrent = fuelCapacity;
    // add return fuelCurrent here;
}

此处缺少return语句:通过查看内容,这不需要任何return语句。把它void

public double printSummary()
//prints out fuelConsumed for each ship
{
    System.out.println(ship1.getName() +" 's total fuel consumption is " + ship1.getFuelConsumed());
    System.out.println(ship2.getName() +" 's total fuel consumption is " + ship2.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship3.getFuelConsumed());
    System.out.println(ship3.getName() +" 's total fuel consumption is " + ship4.getFuelConsumed());
}

此构造函数的定义也有四个参数,但您没有在main()方法中传递对象构造函数中的所有参数。

public Ship(String startName, double startFuelCapacity, double startFuelCurrent, double startFuelConsumed)
{
    // initialise instance variables
    name=startName;
    fuelCapacity=startFuelCapacity;
    fuelCurrent=startFuelCurrent;
    fuelConsumed=startFuelConsumed;
}

    // more errors here: pass all the four arguments.
    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);

答案 2 :(得分:1)

此方法应返回double。如果您不想返回任何内容,请将double替换为void。

public double reFuel()
//refueling each ship returns it to its full fuel capacity
{
    fuelCurrent = fuelCapacity;
}   

答案 3 :(得分:0)

罪魁祸首是

public double printSummary()
//prints out fuelConsumed for each ship
{
System.out.println(ship1.getName() +" 's total fuel consumption is " + ship1.getFuelConsumed());
System.out.println(ship2.getName() +" 's total fuel consumption is " + ship2.getFuelConsumed());
System.out.println(ship3.getName() +" 's total fuel consumption is " + ship3.getFuelConsumed());
System.out.println(ship3.getName() +" 's total fuel consumption is " + ship4.getFuelConsumed()); }

 public double reFuel()
//refueling each ship returns it to its full fuel capacity
{
    fuelCurrent = fuelCapacity;
}   

此函数中的return语句丢失。

将返回值更改为void或返回double