【问题标题】:How to use integers inside of an array as parameters for a method. (Tester)如何使用数组内的整数作为方法的参数。 (测试员)
【发布时间】: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;
    }
}

这个程序的说明是

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

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

【问题讨论】:

  • 让你的方法.calcDistance() 返回一个值(你需要的那个)。将其存储在变量中或直接将其传递给第二个调用函数.clacMPG()
  • 我需要不止一次地使用这个变量,而且方法不止一种。它还会起作用吗?我该怎么做?谢谢
  • 如果您需要帮助,请发布您的所有代码和课程。
  • 是的,如果您需要在同一个循环中使用它,则可以使用变量。
  • 我这样做是正确的,对吧?我需要使用方法和对象计算并打印 4 个填充到屏幕上,特别是我必须使用数组来完成。这可以仅使用每个对象的填充数组来完成吗?这将包含距离和成本等变量以及开始英里和结束英里,允许我在程序结束时打印它吗?还是每个对象的每个变量都需要一个数组?

标签: java arrays oop methods


【解决方案1】:

如果您基本上期望.calcDistance() 方法中计算的值,让您的方法.calcDistance() 返回一个值(您需要的值)。 将其存储在变量中或直接将其传递给第二个调用函数.clacMPG()

由于您已经从 .calcDistance() 返回了一个值,您可以执行类似的操作

int dist = fillUps[index].calcDistance();

现在您可以在您进行的任何其他方法调用中使用此 dist 值,例如

fillUps[index].calcMPG(dist);

您基本上可以使用一个变量,该变量将在每次循环运行时被覆盖。

【讨论】:

    【解决方案2】:

    使用另一个变量:

    int dist=fillUps[index].calcDistance(); 
    //or double, depands on the method's returning value
    
    fillUps[index].calcMPG(dist);
    

    【讨论】:

      【解决方案3】:

      假设 AnnualFuelUse 构造函数参数是:开始英里(里程表)、结束英里、gallonsUsed 和 pricePerGallon。 一种方法是返回距离并作为参数传递给下一个:

          int distance = fillUps[index].calcDistance();
          fillUps[index].calcMPG(distance);
      

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

      **好的。根据您的“编辑”,现在我知道您没有问题,您希望有人为您做作业!!! **

      【讨论】:

        猜你喜欢
        • 2011-03-04
        • 2021-10-19
        • 2015-05-12
        • 2016-10-09
        • 2017-01-24
        • 1970-01-01
        • 2020-06-05
        • 2021-11-20
        • 2019-07-25
        相关资源
        最近更新 更多