我有一项任务是编写一个程序来根据三个项目计算每年的燃料使用量 填满一辆车。我必须使用两个单独的类。这是我的第一堂课,名为AnnualFuelUse。
public class AnnualFuelUse
{
private static int endMiles, startMiles,fillUp, days,distance;
private double gallonsUsed, pricePerGallon,MPG,cost;
//constructor
AnnualFuelUse(int fu, int days, int sm, int em, double gu, double price)
{
fillUp = fu;
days = days;
startMiles = sm;
endMiles = em;
gallonsUsed = gu;
pricePerGallon = price;
distance = 0;
MPG = 0.0;
cost = 0.0;
}
//calculates distance
public void calcDistance ()
{
distance = endMiles - startMiles;
}
public int getDistance(){
return distance;
}
//calculates miles per gallon
public void calcMPG()
{
MPG = distance /gallonsUsed;
}
public double returnMPG(){
return MPG;
}
//calculates total cost
public void totalCost(){
cost= gallonsUsed * pricePerGallon;
}
public double getCost(){
return cost;
}
这是我的第二堂课,名为AnnualFuelUseTester。 (对不起长名,但这是必需的)
public class AnnualFuelUseTester
{
public static void main(String[]args)
{
int fillUp1,fillUp2,fillUp3,days1,days2,days3,startMiles1,startMiles2,startMiles3;
int endMiles1,endMiles2,endMiles3,distance1,distance2,distance3;
double gallons1,gallons2,gallons3,MPG1,MPG2,MPG3,price1,price2,price3;
double cost1,cost2,cost3;
AnnualFuelUseTester[]fuel = {new AnnualFuelUseTester(1,1,45023,45231,10.00,2.95),
new AnnualFuelUseTester(2,4,45231,45480,11.70,2.99),
new AnnualFuelUseTester(3,8,45480,45659,9.30,3.01)};
for (int index = 0; index<fuel.length;index++)
{
fuel[index].calcDistance();
fuel[index].calcMPG();
fuel[index].totalCost();
}
System.out.println(" Fill Up Days Start Miles End Miles Distance Gallons Used MPG Price Cost ");;
for(int index = 0; index < fuel.length; index++)
{
System.out.printf("%5i %5i %5i %5i %5i %5.2d %5.2d %5.2d %5.2d %n",
fillUp,days,startMiles,endMiles,fuel[index].getDistance(),gallonsUsed,fuel[index].getMPG(),price,fuel[index].getCost());
}
}
}
到目前为止,我遇到的问题是,当我运行它时,会出现一条错误,指出AnnualFuelUseTester in 类AnnualFuelUseTester不能应用于给定类型;要求:没有参数; found:int,int,int,int,double,double;原因:实际和正式的参数列表长度不同。
我认为这是关于长度或类型不正确匹配的说法,但据我所知,似乎没问题。我不知道如何解决这个问题,并希望得到一些帮助。
我不知道代码是否能正常运行,即使我已经修复了,但我现在无法再进一步了,所以我试图一步一步。如果您发现任何明显错误,请指出它们,以便我可以弄清楚如何解决它。
我编辑了AnnualFuelUseTester:
public class AnnualFuelUseTester
{
public static void main(String[]args)
{
int fillUp1 = 0 ,fillUp2 = 0,fillUp3 = 0,days1 = 0,days2= 0,days3= 0,startMiles1= 0,startMiles2= 0,startMiles3= 0;
int endMiles1= 0,endMiles2= 0,endMiles3= 0,distance1= 0,distance2= 0,distance3= 0;
double gallons1= 0,gallons2= 0,gallons3= 0,MPG1= 0,MPG2= 0,MPG3= 0,price1= 0,price2= 0,price3= 0;
double cost1= 0,cost2= 0,cost3= 0;
AnnualFuelUse[]fuel = {new AnnualFuelUse(1,1,45023,45231,10.00,2.95),
new AnnualFuelUse(2,4,45231,45480,11.70,2.99),
new AnnualFuelUse(3,8,45480,45659,9.30,3.01)};
for (int index = 0; index<fuel.length;index++)
{
fuel[index].calcDistance();
fuel[index].calcMPG();
fuel[index].totalCost();
}
System.out.println(" Fill Up Days Start Miles End Miles Distance Gallons Used MPG Price Cost ");;
for(int index = 0; index < fuel.length; index++)
{
System.out.printf("%5d %5d %5d %5d %5d %5.2d %5.2d %5.2d %5.2d \n",
fillUp1,days1,startMiles1,endMiles1,fuel[index].getDistance(),gallons1,fuel[index].returnMPG(),price1,fuel[index].getCost());
}
}
}
我之前提到的问题已修复,但现在第31行出错了 - System.out.printf(“%5d%5d%5d%5d%5d%5.2d%5.2d%5.2d%5.2d \ n”, 它编译得很好,但是当我尝试运行它时会出现一个错误“java.util.IllegalFormatPrecisionException:null(在java.util.Formatter $ FormatSpecifier中)。 这是什么意思?
答案 0 :(得分:3)
您正在尝试创建测试类的实例(没有您尝试调用的构造函数),而不是您的AnnualFuelUse类。
更改
AnnualFuelUseTester[]fuel = {new AnnualFuelUseTester(1,1,45023,45231,10.00,2.95),
new AnnualFuelUseTester(2,4,45231,45480,11.70,2.99),
new AnnualFuelUseTester(3,8,45480,45659,9.30,3.01)};
到
AnnualFuelUse[] fuel = {new AnnualFuelUse(1,1,45023,45231,10.00,2.95),
new AnnualFuelUse(2,4,45231,45480,11.70,2.99),
new AnnualFuelUse(3,8,45480,45659,9.30,3.01)};
答案 1 :(得分:1)
您可能想要创建AnnualFuelUse的实例。
AnnualFuelUse[]fuel = {new AnnualFuelUse(1,1,45023,45231,10.00,2.95),
new AnnualFuelUse(2,4,45231,45480,11.70,2.99),
new AnnualFuelUse(3,8,45480,45659,9.30,3.01)};
编译器抱怨它无法找到具有给定参数列表的AnnualFuelUseTester的构造函数。
答案 2 :(得分:1)
这部分就是问题所在。
new AnnualFuelUseTester(1,1,45023,45231,10.00,2.95)
你的意思是将AnnualFuelUse放在那里。 AnnualFuelUseTester有一个不带参数的默认构造函数。
为了将来参考,请发布有关错误的更多信息,例如确切的堆栈跟踪和行号。