0

我在调用程序主体中的方法时遇到问题。

程序规格如下:

setNoOfVehicles():返回拥有的车辆数量。

setWeeklyFuelCost():返回所有拥有车辆的平均每周汽油成本。

calcYearlyFuelCost():接收平均每周燃料成本并返回平均年度燃料成本。

displayFuelCost():接收拥有的车辆数量、平均每周燃料成本和平均每年燃料成本。

main()

调用setWeeklyFuelCost()返回值并将其存储在局部变量中。

displayFuelCost()通过将其作为参数发送来调用,setNoOfVehicles()用于平均每周燃料成本的局部变量,以及对 的调用calcYearlyFuelCost()

Scanner 在全局级别声明

public static void main(String[] args)
{

double x = setWeeklyFuelCost();

displayFuelCost( setNoOfVehicles(), x, calcYearlyFuelCost(x)); //This is the correct parameters I needed to pass thru displayFuelCost(). I didn't know this at the time and this is what I was trying to ask in this post.
}

private static int setNoOfVehicles()
{

System.out.print( "How many vehicles do I own?  " );
 int noOfVehicles = input.nextInt();
return noOfVehicles;
}

private static double setWeeklyFuelCost()
{


System.out.print( "Enter the average weekly fuel cost for my vehicles:  ");
double weeklyFuelCost = input.nextDouble();
return weeklyFuelCost;

}

private static double calcYearlyFuelCost(double weeklyFuelCost)
{

double yearlyFuelCost = 0.0;
yearlyFuelCost = weeklyFuelCost * 52;
return yearlyFuelCost;

}

private static void displayFuelCost( int noOfVehicles, double weeklyFuelCost, double yearlyFuelCost)
{
double difference = yearlyFuelCost - 5044.00;

if( yearlyFuelCost > 5044.00)
{
System.out.printf( "No of Vehicles:  %d\n"
                + "Avg Weekly Fuel Cost:  $%,.2f\n"
                + "Avg Annual Fuel Cost:  $%,.2f\n\n"
                + "I am OVER budget by $%,.2f.", noOfVehicles, weeklyFuelCost, yearlyFuelCost, difference);
}
else if( yearlyFuelCost < 5044.00)
{
difference = difference * -1;
System.out.printf( "No of Vehicles:  %d\n"
                + "Avg Weekly Fuel Cost:  $%,.2f\n"
                + "Avg Annual Fuel Cost:  $%,.2f\n\n"
                + "I am UNDER budget by $%,.2f.  PAARRTY!!! ", noOfVehicles, weeklyFuelCost, yearlyFuelCost, difference);
}
else
{
System.out.printf( "No of Vehicles:  %d\n"
                + "Avg Weekly Fuel Cost:  $%,.2f\n"
                + "Avg Annual Fuel Cost:  $%,.2f\n\n"
                + "I am RIGHT ON BUDGET!", noOfVehicles, weeklyFuelCost, yearlyFuelCost, difference);
}

}

}

最后一个规范是阻碍我的规范,调用 displayFuelCost()

我的问题是我并不确切知道我需要通过哪些参数displayFuelCost()。我知道在问这个问题之前我必须使用上面的变量 x 。

displayFuelCost( setNoOfVehicles(), x, calcYearlyFuelCost(x));是我需要输入的所有内容以使主要工作正常。

4

3 回答 3

2

您调用了一个displayFuelCost()未在您的类中定义的方法。相反,你有一个方法

private static void displayFuelCost( int noOfVehicles, double weeklyFuelCost, double yearlyFuelCost) { ... }

这需要三个参数。

将方法调用更改为

displayFuelCost(1, 100.0, 5200.0);  // sample values

消除错误并获得一些结果。

于 2012-04-08T21:30:37.323 回答
1

您粘贴的代码不包含任何类定义。如果主方法在另一个类中,那么 displayFuelCost 方法,那么你将不得不改变

private static void displayFuelCost( int noOfVehicles, double weeklyFuelCost, double yearlyFuelCost)

公开:

public static void displayFuelCost( int noOfVehicles, double weeklyFuelCost, double yearlyFuelCost)

话虽如此,我不建议您过度使用静态方法。我看不出为什么您不应该使用正确的面向对象样式(或者至少是单例模式,如果它必须看起来是静态的)。

//编辑:

问题在于您的代码的这一部分:

public static void main(String[] args)
{

double x = setWeeklyFuelCost();

displayFuelCost(); //<-- need arguments here!

在您的主函数中,您调用 displayFuelCost 方法,但不提供它需要的参数。当您查看此方法的声明时:

private static void displayFuelCost( int noOfVehicles, double weeklyFuelCost, double yearlyFuelCost)

}

您会看到它需要 3 个参数:一个整数、一个双精度数和另一个双精度数。您必须在调用 displayFuelCost 函数时提供它们。例如像这样:

public static void main(String[] args)
{

double x = setWeeklyFuelCost();

displayFuelCost(1, 2.5, 2.5); //<-- need parameters here!

}

//编辑2:

整个代码还有更多的问题。我添加了一个关于他们的新答案。

于 2012-04-08T21:25:24.567 回答
0

由于我没有扫描仪的代码和课程,我无法证明我的解决方案有效,您必须尝试一下:

public class Test {
    public static void main(String[] args) {

        int vehicleNumber = setNoOfVehicles();
        double costWeek = setWeeklyFuelCost();
        double costYear = calcYearlyFuelCost(costWeek);

        displayFuelCost(vehicleNumber, costWeek, costYear);
    }
// rest of your code
}

但是我必须再次警告你,这可能不是你的老师希望你提供的。他想要一个在主方法中实例化自己的类(例如Test test = new Test()),然后使用实例端方法(即开头没有静态的方法)来完成任务。我会建议你再试一次。;)

于 2012-04-08T21:55:38.470 回答