将数组传递给方法

时间:2015-10-27 00:42:47

标签: java

所以我有两个文件,这是第一个:

public class AnnualFuelUseTester {
    public static void main(String[] args) {
        AnnualFuelUse[] car = {new AnnualFuelUse(1, 1, 3023, 3070, 0.94, 3),
            new AnnualFuelUse(2, 3, 3070, 4224, 24.04, 2.21), new AnnualFuelUse(3, 2, 4224, 4292, 1.48, 2.10)};
        car.showOutput();
    }
}

,第二个是:

public class AnnualFuelUse {
    public void showOutput(AnnualFuelUse[] car) {
        out.printf("%10s%10s%20s%20s%10s%20s%10s%10s%10s%n", "Fill Up", "Days", "Start Miles", "End Miles",
                "Distance", "Gallons Used", "MPG", "Price", "Cost");
        for(AnnualFuelUse car1 : car) {
            out.printf("%5d%5d%5d%5d%5d%5.2f%5.2f%5.2f%5.2f%n", car1.fillup, car1.days, car1.startMiles,
                    car1.endMiles, car1.distance, car1.gallonsUsed, car1.mpg, car1.price, car1.cost);
        }
        calcMinMax(car);
        calcAnnualProjection(car);
    }

我有几个私有变量和另外几个方法,但我认为我展示的内容非常清楚。出于某种原因,我收到错误,说无法解决方法showOutput()和tbh我不知道为什么,文件在同一目录中,一切都是公开的,汽车是AnnualFuelUse[]

1 个答案:

答案 0 :(得分:0)

您需要遍历汽车并在每个汽车上调用showOutput()。