我在调用程序主体中的方法时遇到问题。
程序规格如下:
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));
是我需要输入的所有内容以使主要工作正常。