这是我的第一篇文章,如果做得不对,请见谅。该程序适用于我的 AP 计算机科学课程(我不想要代码示例,只是解释为什么会发生这种情况)。每当我尝试从对象中获取变量时,都会收到一条错误消息,指出该变量不存在(NetBeans IDE)。提前致谢!
这是代码:
public class AnnualFuelTester
{
public static void main(String [] args)
{
AnnualFuelUse Fill1 = new AnnualFuelUse(1, 1, 45023, 45231, 10.00, 2.95);
AnnualFuelUse Fill2 = new AnnualFuelUse(2, 4, 45231, 45480, 11.70, 2.99);
AnnualFuelUse Fill3 = new AnnualFuelUse(3, 8, 45480, 45659, 9.30, 3.03);
AnnualFuelUse [] FillArray = {Fill1, Fill2, Fill3};
System.out.println("Fillup Days Start Miles Distance Gallons Consumed MpG Fuel Price Cost");
**int temp = Fill1.distance;**
System.out.printf ("%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f", );
}
}
(编辑:)年度燃料使用:
public class AnnualFuelUse
{
private int dist = 0;
private double MpG = 0;
private double CoT = 0;
private double totals = 0;
private double projection = 0;
private double min = 0;
private double max = 0;
Annual_Fuel_Use(int fillTemp, int daysTemp, int startTemp, int stopTemp, double gallTemp, double priceTemp){
int fillNumber = fillTemp;
int daysSinceLastFill = daysTemp;
int startMiles = starttemp;
int stopMiles = stoptemp;
int distance = Annual_FuelUse.travelDistance(startMiles, stopMiles);
double fuelConsumed = galltemp;
double mPG = AnnualFuelUse.milesPerGal(dist, fuelConsumed);
double pricePerGallon = pricetemp;
double tripCost = AnnualFuelUse.costOfTrip(pricePerGallon, fuelConsumed);
}
public double getMilesPerGal(){
return MpG;
}
public int getTravelDistance(){
return dist;
}
public double getCostOfTrip(){
return CoT;
}
public double getTotals(){
return totals;
}
public double getProjection(){
return projection;
}
public int travelDistance(int distance1, int distance2){
dist = distance2 - distance1;
return dist;
}
public double milesPerGal(int travelDistance, double fuelConsumed){
MpG = travelDistance / fuelConsumed;
return MpG;
}
public double costOfTrip(double pricePerGal, double fuelConsumed){
CoT = pricePerGal * fuelConsumed;
return CoT;
}
public double totals(double dat1, double dat2, double dat3){
totals = dat1 + dat2 + dat3;
return totals;
}
public double projection(int days,double projected){
projection = (projected / days) * 365;
return projection;
}
public double dataMin(int dat1, int dat2, int dat3){
if (dat1 <= dat2){
min = dat1;
}
if (min <= dat3){
return min;
} else {
return dat3;
}
}
public double dataMax(int dat1, int dat2, int dat3){
if (dat1 >= dat2){
max = dat1;
}
if (max >= dat3){
return max;
} else {
return dat3;
}
}
}