-4

プログラムを実行しましたが、出力にが表示されます0。私が間違ったことを理解できますか?私の評価の指示はこれを尋ねます。

package snippet;

import java.util.ArrayList;

public class CO2FromElectricity {
    // declaration of private instance variables

    /**
     * Default constructor to create an object from the CO2FromElectricity class.
     */
    CO2FromElectricity() {
    }

    /**
     * A mutator method which calculates the average annual electricity bill.
     * 
     * @param monthlyBill
     *            an ArrayList containing the monthly bills for home electricity use.
     * @return the average monthly electricity bill.
     */
    public double calcAverageBill(ArrayList<Double> monthlyBill) {
        monthlyBill.add(279.41);
        monthlyBill.add(238.03);
        monthlyBill.add(248.64);
        monthlyBill.add(258.73);
        monthlyBill.add(395.48);
        monthlyBill.add(419.91);
        monthlyBill.add(431.15);
        monthlyBill.add(407.56);
        monthlyBill.add(417.14);
        monthlyBill.add(308.35);
        monthlyBill.add(337.91);
        monthlyBill.add(320.77);
        double sum = 0.0;
        double monthlyTotal = 0.0;

        for (int i = 0; i < monthlyBill.size(); i++) {
            sum += monthlyBill.get(i);
        }

        return monthlyTotal / monthlyBill.size();
    }

    /**
     * A mutator method which calculates the average annual price of electricity.
     * 
     * @param monthlyPrice
     *            an ArrayList containing the monthly price of electricity per kilowatthour.
     * @return the average monthly price of electricity.
     */
    public double calcAveragePrice(ArrayList<Double> monthlyPrice) {
        monthlyPrice.add(0.1117);
        monthlyPrice.add(0.1107);
        monthlyPrice.add(0.1110);
        monthlyPrice.add(0.1113);
        monthlyPrice.add(0.1135);
        monthlyPrice.add(0.1138);
        monthlyPrice.add(0.1217);
        monthlyPrice.add(0.1215);
        monthlyPrice.add(0.1216);
        monthlyPrice.add(0.1228);
        monthlyPrice.add(0.1209);
        monthlyPrice.add(0.1192);
        double sum = 0.0;
        double monthlyTotal = 0.0;

        for (int i = 0; i < monthlyPrice.size(); i++) {
            sum += monthlyPrice.get(i);
        }
        return monthlyTotal / monthlyPrice.size();
    }

    /**
     * A mutator method which calculates the annual home CO2 emission from electricity.
     * 
     * @param avgBill
     *            the average monthly home electricity bill.
     * @param avgPrice
     *            the average monthly price of home electricity.
     * @return the annual home CO2 emission from home electricity use.
     */
    public double calcElectricityCO2(double avgBill, double avgPrice) {
        return (avgBill / avgPrice) * 1.37 * 12;
    }
}

出力は次のようになります。私が間違ったことを見てください。

Average Monthly Electricity Bill: 191.23  
Average Monthly Electricity Price: 0.11  
Annual CO2 Emissions from Electricity Usage: 28667.1 pounds  
4

2 に答える 2

1

月間合計は常に0.0です。したがって、関数は0.0/sumを返します

于 2013-01-14T19:13:59.633 に答える
0

次のように書くため、出力は 0 です。

double monthlyTotal = 0.0;

私はあなたがしたいと思います

 return  sum/monthlyPrice.size();
于 2013-01-14T19:21:03.110 に答える