用两个文件程序用java计算

时间:2014-01-02 03:03:19

标签: java date-arithmetic

我有一个关于如何用java计算的问题。在我的情况下,我认为我正在进行正确的计算,但是当我运行它时,我会得到一些0和一些荒谬的数字。我怎样才能解决这个问题?任何帮助将不胜感激。以下是我的代码:

public class CO2Footprint {

    private int NumberOfPeople, CO2PerPerson, lightBulbs, months;
    private double TotalEmission, ReductionPerPaper, ReductionPerPlastic, 
            ReductionPerGlass, ReductionPerCan, TotalReduction, 
            FamilyNetEmission, annualGasoline, electricityBill
            , lightBulbsRecycled, annualFuelUse, averageMonthlyCostElec, 
            emissionFactor, monthlyCostSeptElec, monthlyCostOct, monthlyCostNov,
            averagePricePerKilowatt, totalWaste, elecPrice, NetEmission;
    private boolean paper, plastic, glass, can;

    CO2Footprint(int numPeople, boolean bpaper, boolean bplastic, boolean 
            bglass, boolean bcans, double annuallGasoline
    , double electricityyPrice, int lighttBulbs){
        NumberOfPeople = 0;
        paper = bpaper;
        plastic = bplastic;
        glass = bglass;
        can = bcans;
        ReductionPerPaper = 184;
        CO2PerPerson = 1018;
        ReductionPerPlastic = 25.6;
        ReductionPerGlass = 46.6;
        ReductionPerCan = 165.8;
        TotalEmission = 0;
        annualGasoline = 16.5;
        electricityBill = 0;
        lightBulbs = 10;
        lightBulbsRecycled = 0;
        annualFuelUse = 0;
        averageMonthlyCostElec = 0;
        emissionFactor = 1.37;
        monthlyCostSeptElec = 551.51;
        monthlyCostOct = 392.84;
        monthlyCostNov = 445.42;
        months = 12;
        averagePricePerKilowatt = 4.83;
        totalWaste = 0;
        NetEmission = 0.0;
    }

    // method for calculating total emission
    public double totalEmission(){
        TotalEmission = NumberOfPeople * CO2PerPerson;
        return TotalEmission;
    }

    // method for calculating CO2 reduction
    public double reduction(){
        TotalReduction = 0;
        ReductionPerPaper = 0;
        ReductionPerPlastic = 0;
        ReductionPerGlass = 0;
        ReductionPerCan = 0;
        if(paper = true){
            ReductionPerPaper = 184;
        }else{
            ReductionPerPaper = 0;
        }
        if(plastic = true){
            ReductionPerPlastic = 25.6;
        }else{
            ReductionPerPlastic = 0;
        }
        if(glass = true){
            ReductionPerGlass = 46.6;
        }else{
            ReductionPerGlass = 0;
        }
        if(can = true){
            ReductionPerCan = 165.8;
        }else{
            ReductionPerCan = 0;
        }
        TotalReduction = ReductionPerPaper + ReductionPerPlastic + 
                ReductionPerGlass + ReductionPerCan + (lightBulbs * 1.37 * 73);
        return TotalReduction;
    }

    public double calcAnnualFuelUseGallonCO2(){
        annualFuelUse = annualGasoline * 12 * 12;
        return annualFuelUse;
    }

    public double calcElectricityBill(){
        return (averageMonthlyCostElec / averagePricePerKilowatt) * emissionFactor * months;
    }

    public double electrictyPrice(){
        return averagePricePerKilowatt;
    }

    public double calcWaste(){
        TotalReduction = 0;
        ReductionPerPaper = 0;
        ReductionPerPlastic = 0;
        ReductionPerGlass = 0;
        ReductionPerCan = 0;
        if(paper = true){
            ReductionPerPaper = 184;
        }else{
            ReductionPerPaper = 0;
        }
        if(plastic = true){
            ReductionPerPlastic = 25.6;
        }else{
            ReductionPerPlastic = 0;
        }
        if(glass = true){
            ReductionPerGlass = 46.6;
        }else{
            ReductionPerGlass = 0;
        }
        if(can = true){
            ReductionPerCan = 165.8;
        }else{
            ReductionPerCan = 0;
        }
        totalWaste = ReductionPerPaper + ReductionPerPlastic + 
                ReductionPerGlass + ReductionPerCan;
        return totalWaste;
    }

    public double calcBulbsEmissionReduction(){
        return lightBulbs * 1.37 * 73;
    }

    public double calcNetEmission(){
        NetEmission = TotalEmission - CO2PerPerson;
        return NetEmission;
    }


    }

If it helps, below is the code for how I called the methods:


    import java.util.ArrayList;

    public class CO2FootprintTester {

    public static void main(String args[]){

        ArrayList<CO2Footprint> CO2 = new ArrayList<CO2Footprint>();

        CO2.add(new CO2Footprint(1, true, true, true, true, 16.5, 4.83, 10));

        CO2Footprint data;

        for(int i = 0; i < CO2.size(); i++){
            data = CO2.get(i);
            data.calcAnnualFuelUseGallonCO2();
            data.calcBulbsEmissionReduction();
            data.calcElectricityBill();
            data.calcWaste();
            data.electrictyPrice();
            data.reduction();
            data.totalEmission();
        }

        // create table
        System.out.println("___________________________________________________"
                + "_____________________________________________________________________________");
        System.out.printf("%65s%44s%n", "        Pounds of CO2 Emitted From:        |",
                "            Pounds of CO2 Reduced From:            ");
        System.out.println("___________________________________________________"
                + "_____________________________________________________________________________");
        System.out.printf("%1s%25s%25s%25s%25s%n%n", "Gas", "Electricity", 
                "Waste", "Recycling", "New Bulbs");

        // call methods
        for(int i = 0; i < CO2.size(); i++){
            data = CO2.get(i);
            System.out.printf("%5.5f%15.5f%15.5f%15.5f%15.5f", data.calcAnnualFuelUseGallonCO2(), data.calcElectricityBill(), 
                    data.totalEmission(), data.calcNetEmission(), data.calcBulbsEmissionReduction());//, 
                    //data.calcElectricityBill(), data.totalEmission(), data.calcNetEmission(), 
                    //data.calcBulbsEmissionReduction());
        }
    }

    }

1 个答案:

答案 0 :(得分:0)

好像您遇到了使用double数据类型精度的问题。 double数据类型有时在货币价值等精确计算中很麻烦。

建议使用BigDecimal来确保算术运算时的精度。

可在此post

找到更多信息