如何在数组内使用整数作为方法的参数。 (测试器)

时间:2016-01-16 20:43:37

标签: java arrays oop methods

我计算一个整数并使用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;
    }
}

该计划的说明是

  1. 如果您尚未在Mod08中创建8.08年度燃料使用项目 分配文件夹,请立即执行。
  2. 请务必在Mod08文档文件夹中保存这些说明的副本。
  3. 为您的笔记本打印一份副本。
  4. 在尝试分配之前,请仔细阅读说明。
  5. 创建两个名为的类 AnnualFuelUseTester和AnnualFuelUse 在新创建的项目文件夹中。
  6. 使用您收集的填充数据 为你的车(或家用车)和计算 总距离,加仑使用和成本 气体。
  7. 确定最小值和最大值 距离值,每加仑英里数和 价钱。 (回想一下Integer类的常量 MIN_VALUE和MAX_VALUE。该 Double类也有类的常量 同名。)
  8. 计算距离的年度预测, 使用的加仑数,每加仑英里数和成本 根据您收集的数据。

  9. 每个填充应该被视为一个对象,你的程序设计应该是 基于一个对象数组。使用本课程中的演示程序作为模型 如何创建和处理对象数组。

3 个答案:

答案 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);

我无法在您的代码中看到您如何整合数据以及如何预测未来的数据。

**确定。基于你的“编辑”,现在我知道你没有问题,你想要有人为你做功课! **