当我运行我的代码时,我收到此错误,我无法弄清楚原因。 错误是“找不到符号 - 方法calcAverageBill(java.util.ArrayList)”
程序是用java编写的。 头等舱:
import java.util.ArrayList;
public class CO2Footprint
{
private int myNumPeople, myBulbsReplaced;
private boolean myPaper, myPlastic, myGlass, myCans;
private double myGalsUsed, myElectricBill, myElectricPrice, billSum, priceSum;
/**
* Constructor for objects of type CO2Footprint
* @param galsUsed anuual gasoline used
* @param electricBill average electricity bill
* @param electricPrice average electricity price
* @param numPeople number of people in home
* @param paper whether or not paper is recycled
* @param plastic whether or not plastic is recycled
* @param glass whether or not glass is recycled
* @param cans whether or not cans are recycled
* @param bulbsReplaced number of light bulbs replaced
*/
CO2Footprint(double galsUsed, double electricBill, double electricPrice, int numPeople,
boolean paper, boolean plastic, boolean glass, boolean cans, int bulbsReplaced) {
myNumPeople = numPeople;
myBulbsReplaced = bulbsReplaced;
myPaper = paper;
myPlastic = plastic;
myGlass = glass;
myCans = cans;
myGalsUsed = galsUsed;
myElectricBill = electricBill;
myElectricPrice = electricPrice;
}
public double calcAverageBill(ArrayList<Double> myElectricBill) {
for(int i = 0; i < myElectricBill.size(); i++) {
billSum += myElectricBill.get(i);
}// **** insert code for method here ****//
return billSum/3.0;
}
public double calcAveragePrice (ArrayList<Double> myElectricPrice)
{
for (int i = 0; i < myElectricPrice.size(); i++) {
priceSum += myElectricPrice.get(i);
}// **** insert code for method here ****//
return priceSum/3.0;
}
public double calcElectricityCO2 (double avgBill, double avgPrice)
{
return (avgBill/avgPrice)*1.37*12;
// **** insert code for method here ****//
}
}
第二课:
import java.util.ArrayList;
public class CO2FootPrintTester
{
public static void main(String[] args) {
ArrayList<CO2Footprint> CO2 = new ArrayList<CO2Footprint>();
ArrayList<Double> myElectricBill = new ArrayList<Double>(3);
ArrayList<Double> myElectricPrice = new ArrayList<Double>(3);
myElectricBill.add(209.60);
myElectricBill.add(249.68);
myElectricBill.add(222.59);
myElectricPrice.add(209.70/2464);
myElectricPrice.add(249.68/2948);
myElectricPrice.add(222.59/2621);
double avgBill = CO2.calcAverageBill(myElectricBill);
double avgPrice = CO2.calcAveragePrice(myElectricPrice);
double emissions = CO2.calcElectricityCO2(avgBill, avgPrice);
//***********************************************************
System.out.println("| Pounds of CO2 | Pounds of CO2 | |");
System.out.println("| Emmitted from | Reduced from | | ");
System.out.println("| Gas | Electricity | Waste | Recycling | New Bulbs | CO2 Footprint |");
System.out.println("|-------|-------------------|-----------|--------------|------------|----------------|");
System.out.print(emissions);
//***********************************************************
}
}
答案 0 :(得分:0)
calcAverageBill
是您CO2Footprint
课程中定义的方法,但您尝试将其调用为ArrayList
的实例。
ArrayList<CO2Footprint> CO2 = new ArrayList<CO2Footprint>();
...
double avgBill = CO2.calcAverageBill(myElectricBill);
ArrayList
没有这样的方法。
目前还不清楚CO2 ArrayList的用途是什么,因为你永远不会添加任何元素。也许您应该创建CO2Footprint
而不是ArrayList<CO2Footprint>
的实例,并为该实例调用calcAverageBill
。