我计算一个整数并使用for循环内的方法将其分配到数组中,然后for循环中的下一个方法需要将前一个整数计算为参数我将其声明为一个修复该问题的双精度但是现在我需要打印结果并且我有同样的问题,在打印时我在方法参数中放了什么,因为在第一个循环中的每个循环之后擦除了变量。
这是主要的方法类:
public class AnnualFuelTester {
public static void main(String args[]) {
//declaration of variables
int endMiles, startMiles;
double gallonsUsed, pricePerGallon;
//initialization of an array of objects
AnnualFuelUse[] fillUps = {
new AnnualFuelUse(45023, 45231, 10.00, 2.95),
new AnnualFuelUse(45231, 45480, 11.70, 2.99),
new AnnualFuelUse(45480, 45659, 9.30, 3.03),
new AnnualFuelUse(45659, 45961, 14.90, 3.05)
};
//call methods
for (int index = 0; index < fillUps.length; index++) {
double distance = fillUps[index].calcDistance();
fillUps[index].calcMPG(distance);
fillUps[index].getStartMiles();
fillUps[index].getEndMiles();
fillUps[index].getGallons();
fillUps[index].totalCost(distance);
}
//print results
System.out.printf(" %15s %15s %15s %15s %15s %15s %15s %15s %15s", "Fill Up", "Days", "Start Miles", "End Miles", "Distance", "Gallons", "Miles/Gal", "Gallons/Miles", "Price", "Total Cost\n");
for (int index = 0; index < fillUps.length; index++) {
System.out.printf("%15i %15i %15s %15s %15d %15d %15d %15d %15d", index, index, fillUps[index].getStartMiles(), fillUps[index].getEndMiles(), fillUps[index].calcDistance(), fillUps[index].getGallons(), fillUps[index].calcMPG(distance), fillUps[index].totalCost(distance), "\n");
}
}
}
这是带有方法的类:
public class AnnualFuelUse {
//private instance variables
private int myEndMiles, myStartMiles;
private double myGallonsUsed, myPricePerGallon;
AnnualFuelUse(int sm, int em, double gu, double ppg) {
myEndMiles = em;
myStartMiles = sm;
myGallonsUsed = gu;
myPricePerGallon = ppg;
}
//distance driven
public double calcDistance() {
return myEndMiles - myStartMiles;
}
//calculate miles per gallon
public double calcMPG(double distance) {
return distance / myGallonsUsed;
}
//calculate gallons per mile
public double calcGPM(double distance) {
return (distance / myGallonsUsed) / 100;
}
//calculate total cost
public double totalCost(double distance) {
return myPricePerGallon * distance;
}
//getter start miles
public int getStartMiles() {
return myStartMiles;
}
//getter end miles
public int getEndMiles() {
return myEndMiles;
}
//getter gallons used
public double getGallons() {
return myGallonsUsed;
}
//getter price per gallon
public double getPricePerGallon() {
return myPricePerGallon;
}
}
该计划的说明是
计算距离的年度预测, 使用的加仑数,每加仑英里数和成本 根据您收集的数据。
每个填充应该被视为一个对象,你的程序设计应该是 基于一个对象数组。使用本课程中的演示程序作为模型 如何创建和处理对象数组。
答案 0 :(得分:2)
如果您基本上期望使用.calcDistance()
方法计算的值,请让您的方法.calcDistance()
返回一个值(您需要的值)。
将其存储在变量中或直接将其传递给第二个调用函数.clacMPG()
由于您已经从.calcDistance()
返回了值,因此您可以执行类似
int dist = fillUps[index].calcDistance();
现在您可以在您进行的任何其他方法调用中使用此dist
值,例如
fillUps[index].calcMPG(dist);
你基本上可以使用一个变量,每次循环运行时都会被覆盖。
答案 1 :(得分:1)
使用另一个变量:
int dist=fillUps[index].calcDistance();
//or double, depands on the method's returning value
fillUps[index].calcMPG(dist);
答案 2 :(得分:1)
假设AnnualFuelUse构造函数参数是:start miles(里程表),end miles,gallonsUsed和pricePerGallon。 一种方法是返回距离并作为参数传递给下一个:
int distance = fillUps[index].calcDistance();
fillUps[index].calcMPG(distance);
我无法在您的代码中看到您如何整合数据以及如何预测未来的数据。
**确定。基于你的“编辑”,现在我知道你没有问题,你想要有人为你做功课! **