gpt4 book ai didi

JAVA数组与对象错误问题(初学者)

转载 作者:行者123 更新时间:2023-12-01 17:01:49 28 4
gpt4 key购买 nike

我的任务是编写一个程序,根据以下三个因素来预测您的年度燃料使用量汽车加油。我必须使用两个单独的类。这是我的第一个类,名为 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 不能应用于给定类型;必需:无参数;发现: 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(in java.util.Formatter$FormatSpecifier)”。这是什么意思?

最佳答案

您正在尝试创建测试类的实例(它没有您尝试调用的构造函数),而不是您的 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)};

关于JAVA数组与对象错误问题(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27196986/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com