数组程序获取公共方法的错误

时间:2013-01-16 18:36:05

标签: java

尊敬的帮助者 我写了这个程序,它显示了下面列出的问题......

public class CO2Footprint
{
       CO2Footprint()
    {
    }




      public static  void calculateGasEmissions(int [] gas){
        double[] gasFootprint = new double[5];
        for(int counter = 0; counter <= 5; counter++){
            gasFootprint[counter] = 19.3565 * gas[counter];
                }
        }
      public double getGasEmission(int [] gasFootprint)
      {for(int counter = 0; counter <= 5; counter++){
          return gasFootprint[counter];

      }

      public void calculateElectricityEmissions(int [] averageElectricBill, int [] averageElecPrice){
        double[] electricityEmissions = new double[5];
        for(int counter = 0; counter <= 5; counter++){
             electricityEmissions[counter] = (averageElectricBill[counter]/averageElecPrice[counter]) * 1.37 * 12;
                }
            }


    public void calcWasteReduction(int [] lightsTotal){
        double[] emissionReductions = new double[5];
        for(int counter = 0; counter <=5; counter++){
            emissionReductions[counter] = lightsTotal[counter] * 1.37 * 73;
        }
}



      public void calcGrossWasteEmission(int [] numberOfPeople) {
            double[] grossWasteEmission = new double[5];
           for(int counter = 0; counter <=5; counter++){
                grossWasteEmission[counter] = numberOfPeople[counter] * 1018;
    }

}


    public void calcNetWasteReduction(boolean [] cans,boolean [] plastic,boolean [] glass,boolean [] paper, double[] grossWasteEmission,int [] numberOfPeople, int[] emissionReductions) {

           for (int counter = 0; counter<=5; counter++){
               if (cans[counter]){
               double canReductions = 165.8;
              grossWasteEmission[counter] -= (canReductions * numberOfPeople[counter]) - emissionReductions[counter];
                   }
                }


           for (int counter = 0; counter<=5; counter++){
           if(glass[counter]){
               double glassReductions = 46.6 ;
               grossWasteEmission[counter] -= (glassReductions * numberOfPeople[counter]) - emissionReductions[counter];
                }
            }

             for (int counter = 0; counter<=5; counter++){
              if(plastic[counter]){
               double plasticReductions = 25.6;
              grossWasteEmission[counter] -= (plasticReductions * numberOfPeople[counter]) - emissionReductions[counter];
               }
            }

            for (int counter = 0; counter<=5; counter++){
             if(paper[counter]){
               double paperReductions = 184.0;
               grossWasteEmission[counter] -= (paperReductions * numberOfPeople[counter]) - emissionReductions[counter];
                }
           }





        }

}

我在这条线上只遇到一个问题......

public void calculateElectricityEmissions(int [] averageElectricBill, int [] averageElecPrice)

我不知道为什么我会在那里遇到问题。我宣布了所有的变数。它说非法开始表达。

3 个答案:

答案 0 :(得分:2)

问题与您上次提出的问题相同,解决方案也是如此。

答案 1 :(得分:1)

您忘了关闭上一个方法的大括号: -

public double getGasEmission(int [] gasFootprint)
{
      for(int counter = 0; counter <= 5; counter++){
          return gasFootprint[counter];

      }
} // You missed this brace after the for loop brace

还有一个建议: -

当您在方法中声明新数组或使用循环条件时,请勿对大小进行硬编码 - 5 here。最好使用传递的数组的大小,如下所示: -

public static  void calculateGasEmissions(int [] gas){
    double[] gasFootprint = new double[gas.length];  // Replace 5 here
    // For loop condition should be `<` and not `<=` to avoid `IndexOutOfBounds`
    for(int counter = 0; counter < gas.length; counter++) { // and here
        gasFootprint[counter] = 19.3565 * gas[counter];
    }
}

关于第一种方法中的return语句,您可能需要在for loop内计算一些值,然后在for循环结束后返回它。

答案 2 :(得分:0)

在这一行

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

它应该是“&lt;”而不是“&lt; =”。我怀疑你有同样的错误。