使用OOP

时间:2016-01-17 16:33:17

标签: java oop arraylist methods cannot-find-symbol

我有一个这个程序的指南,我应该以某种方式使用对象CO2并添加值。我不知道在哪里实现在开始时声明的对象CO2我得到一个找不到符号每当我尝试添加到列表时出错。我正在使用课程来实现这一目标。

来自电力测试代码的二氧化碳

/**
 * @purpose: Calculate yearly CO2 emissions from electricity 8.10
 *
 * @author:
 * @version:
 */
import java.util.ArrayList;

public class CO2FromElectricityTester {

    public static void main(String[] args) {
        CO2FromElectricity CO2 = new CO2FromElectricity();

        ArrayList<Double> monthlyBill = new ArrayList<Double>(3);

        ArrayList<Double> monthlyPrice = new ArrayList<Double>(3);

        // Values to add to the monthly bill or use your own:
        // 209.60, 249.68. 222.59
        monthylyBill.add(209.60);
        monthylyBill.add(249.68);
        monthylyBill.add(222.59);

        // Values to add to the monthly Price or use your own:
        // (209.70 / 2464), (249.68 / 2948), (222.59 / 2621)
        monthylyPrice.add(0.24);
        monthylyPrice.add(0.35);
        monthylyPrice.add(0.27);

        double avgBill = CO2.calcAverageBill(monthlyBill);
        double avgPrice = CO2.calcAveragePrice(monthlyPrice);

        double emissions = CO2.calcElectricityCO2(avgBill, avgPrice);

        System.out.printf("Average Monthly Electricity Bill: %6.2f%n", avgBill);
        System.out.printf("Average Monthly Electricity Price: %4.2f%n", avgPrice);
        System.out.printf("Annual CO2 Emissions from Electricity Usage:   %7.1f pounds", emissions);
    }
}

来自电力类的二氧化碳

import java.util.ArrayList;

public class CO2FromElectricity

{
    CO2FromElectricity() {
        // default constructor should be used.
    }

    public double calcAverageBill(ArrayList<Double> monthlyBill) {
        double sum = 0;
        for (int i = 0; i < monthlyBill.size(); i++) {
            sum += monthlyBill.get(i);
        }
        return ((double) sum) / monthlyBill.size();
    }

    public double calcAveragePrice(ArrayList<Double> monthlyPrice) {
        double sum = 0;
        for (int i = 0; i < monthlyPrice.size(); i++) {
            sum += monthlyPrice.get(i);
        }
        return ((double) sum) / monthlyPrice.size();
    }

    public double calcElectricityCO2(double avgBill, double avgPrice) {
        return ((double) (avgBill / avgPrice) * 1.37 * 12);
    }
}

2 个答案:

答案 0 :(得分:1)

这是一个错字。 monthlyPrice是您声明的变量名称,但在您的代码中,您要添加一些名为monthylyBill的变量,该变量不存在。

答案 1 :(得分:1)

问题是一个简单的错字:

monthylyPrice.add(0.24);
 monthylyPrice.add(0.35);
 monthylyPrice.add(0.27);

应该是&#34; monthlyPrice&#34;不是&#34;月 y lyPrice&#34;