0

我的程序是:

public class CO2FootPrintTester {
  public static void main(String[] args) {


    //declaration of variables 

    int[] numberOfPeople = new int[5];
    numberOfPeople[0] = 3;
    numberOfPeople[1] = 6;
    numberOfPeople[2] = 2;
    numberOfPeople[3] = 10;
    numberOfPeople[4] = 1;

    double[] avgElecBill = new double[5];
    avgElecBill[0] = 227.29;
    avgElecBill[1] = 213.28;
    avgElecBill[2] = 234.78;
    avgElecBill[3] = 256.04;
    avgElecBill[4] = 221.96;

    for (int counter = 0; counter <= 5; counter++) {
      int totalAverageElectricBill = 0;
      totalAverageElectricBill += avgElecBill[counter];
      int averageElectricBill = totalAverageElectricBill / 5;
    }


    boolean[] cans = new boolean[5];
    cans[0] = true;
    cans[1] = false;
    cans[2] = true;
    cans[3] = false;
    cans[4] = true;

    boolean[] glass = new boolean[5];
    glass[0] = true;
    glass[1] = false;
    glass[2] = true;
    glass[3] = false;
    glass[4] = true;

    boolean[] plastic = new boolean[5];
    plastic[0] = true;
    plastic[1] = true;
    plastic[2] = false;
    plastic[3] = false;
    plastic[4] = true;

    boolean[] paper = new boolean[5];
    paper[0] = true;
    paper[1] = false;
    paper[2] = true;
    paper[3] = false;
    paper[4] = true;

    int[] numLights = new int[5];
    numLights[0] = 9;
    numLights[1] = 3;
    numLights[2] = 5;
    numLights[3] = 1;
    numLights[4] = 8;

    for (int counter = 0; counter <= 5; counter++) {
      int[] lightsTotal = new int[5];
      lightsTotal[counter] += numLights[counter];
    }


    int[] gas = new int[5];
    gas[0] = 2604;
    gas[1] = 3029;
    gas[2] = 1745;
    gas[3] = 3590;
    gas[4] = 1362;

    for (int counter = 0; counter <= 5; counter++) {
      int gasTotal = 0;
      gasTotal += gas[counter];
    }

    double[] avgElecPrice = new double[5];
    avgElecPrice[0] = .084;
    avgElecPrice[1] = .081;
    avgElecPrice[2] = .085;
    avgElecPrice[3] = .084;
    avgElecPrice[4] = .086;

    for (int counter = 0; counter <= 5; counter++) {
      int totalAverageElectricPrice = 0;
      totalAverageElectricPrice += avgElecPrice[counter];
      int averageElecPrice = 0;
      averageElecPrice = totalAverageElectricPrice / 5;
    }

    double[] gasFootprint = new double[5];
    double[] electricityEmissions = new double[5];
    double[] emissionReductions = new double[5];
    double[] grossWasteEmission = new double[5];


    //call methods
    for (int counter = 0; counter <= 4; counter++) {

      gasFootprint[counter] = CO2Footprint.calculateGasEmissions(gas);
      electricityEmissions[counter] = CO2Footprint.calculateElectricityEmissions(averageElectricBill, averageElecPrice);
      emissionReductions[counter] = CO2Footprint.calcNetWasteReduction(cans, plastic, glass, paper, grossWasteEmission, numberOfPeople);
      grossWasteEmission[counter] = CO2Footprint.calcGrossWasteEmission(numberOfPeople);
    }


    //print results

    System.out.println("|               Pounds of CO2             |      Pounds of CO2         |                       |");
    System.out.println("|               Emmited from              |      Reduced from          |                       |");
    System.out.println("|   Gas   |      Electricity  |   Waste   |   Recycling  |  New Bulbs  |    CO2 Footprint      |");


  }
}

我在以下两行遇到问题:

gasFootprint[counter] = CO2Footprint.calculateGasEmissions(gas);

grossWasteEmission[counter] = CO2Footprint.calcGrossWasteEmission(numberOfPeople);

第 1 行:它说 required doublefound void

calcGrossWasteEmission(int[])第 2 行:不能从静态上下文引用非静态方法。需要double找到void

我不知道为什么它说我需要double。我认为它已经是一个double.

请帮忙。提前致谢。

4

3 回答 3

1

我必须看到 CO2Footprint 类才能确定,但​​我猜 calculateGassEmissions(gas) 是一种public void方法而不是public double

另外我必须假设它calcGrossWasteEmission是一个实例方法而不是一个静态方法。

于 2013-01-16T19:43:35.970 回答
1
gasFootprint[counter] = CO2Footprint.calculateGasEmissions(gas);

似乎calculateGasEmissionsomCO2Footprint不是静态方法。

如果是这种情况,那么您需要实例化CO2Footprint类并调用calculateGasEmissions该实例。

例如:

CO2Footprint co2Instance = new CO2Footprint();
co2Instance.calculateGasEmissions(gas);

第二题:

数组索引从 0 开始,因此,您的for循环将通过ArrayIndexOutofBoundsException.

 for (int counter = 0; counter <=5; counter++)

应该:

   for (int counter = 0; counter <5; counter++)
于 2013-01-16T19:41:38.907 回答
1
for (int counter = 0; counter <=5; counter++)

这应该是:

for (int counter = 0; counter < 5; counter++)

因为在大小为 5 的数组中没有索引 5(即数组从 0 开始)。

于 2013-01-16T19:41:40.567 回答