【发布时间】:2016-04-22 06:40:15
【问题描述】:
我计算一个整数并使用 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;
}
}
这个程序的说明是
- 如果您尚未在 Mod08 中创建 8.08 年度燃料使用项目 作业文件夹,请立即执行。
- 请务必将这些说明的副本保存在 Mod08 Documents 文件夹中。
- 为您的笔记本打印一份副本。
- 在尝试分配之前仔细阅读说明。
- 创建两个名为 年度燃料使用测试仪和年度燃料使用 在新创建的项目文件夹中。
- 使用您一直在收集的填充数据 为您的汽车(或家用汽车)计算 总距离、使用的加仑和成本 气体。
- 确定最小值和最大值 距离值、英里/加仑和 价钱。 (回想一下 Integer 类的常量 MIN_VALUE 和 MAX_VALUE。这 双类也有类常量 同名。)
计算距离的年度预测, 使用的加仑数、每加仑英里数和成本 根据您收集的数据。
每个填充都应视为一个对象,您的程序设计应该是 基于对象数组。使用本课中的演示程序作为模型 如何创建和处理对象数组。
【问题讨论】:
-
让你的方法
.calcDistance()返回一个值(你需要的那个)。将其存储在变量中或直接将其传递给第二个调用函数.clacMPG() -
我需要不止一次地使用这个变量,而且方法不止一种。它还会起作用吗?我该怎么做?谢谢
-
如果您需要帮助,请发布您的所有代码和课程。
-
是的,如果您需要在同一个循环中使用它,则可以使用变量。
-
我这样做是正确的,对吧?我需要使用方法和对象计算并打印 4 个填充到屏幕上,特别是我必须使用数组来完成。这可以仅使用每个对象的填充数组来完成吗?这将包含距离和成本等变量以及开始英里和结束英里,允许我在程序结束时打印它吗?还是每个对象的每个变量都需要一个数组?