JAVA对象数组程序输出错误(初学者)

时间:2014-11-30 15:26:34

标签: java

我有一项任务来计算一年内产生的二氧化碳量 一个家庭,并比较回收如何减少其二氧化碳足迹。 程序中有两个类,CO2FromWaste和CO2FromWasteTester。

头等舱,CO2FromWaste是:

public class CO2FromWaste
{
    //declaration of private instance variables
    private int numPeople;
    private boolean Paper, Plastic, Glass, Cans;
    private double grossWasteEmission, wasteReduction, netWasteReduction;

    //constructor 
    CO2FromWaste(int people, boolean paper, boolean plastic, boolean glass, boolean cans){
        numPeople = people;
        Paper = paper;
        Plastic = plastic;
        Glass = glass;
        Cans = cans;
        grossWasteEmission = 0.0;
        wasteReduction = 0.0;
        netWasteReduction = 0.0;
    }
    public void calcGrossWasteEmission(){
        grossWasteEmission = numPeople * 1018;

    }
    public double getGrossWasteEmission(){
        return grossWasteEmission;
    }
    public void calcWasteReduction(){
        if (Paper == true){
            wasteReduction = numPeople * 184;
        }
        if (Plastic == true){
            wasteReduction += (numPeople * 25.6);
        }
        if (Glass == true){
            wasteReduction+= (numPeople*46.6);
        }
        if (Cans == true){
            wasteReduction+=(numPeople*165.8);
        }

    }
    public double getWasteReduction()
    {
        return wasteReduction;
    }
    public void calcNetWasteReduction(){
        netWasteReduction = grossWasteEmission = wasteReduction;

    }
    public double getNetWasteReduction(){
        return netWasteReduction;
    }
    public int getNumPeople(){
        return numPeople;
    }
    public boolean getPaper(){
        return Paper;
    }
    public boolean getPlastic(){
        return Plastic;
    }
    public boolean getGlass(){
        return Glass;
    }
    public boolean getCans(){
        return Cans;
    }
}

第二类CO2FromWasteTester是:

import java.util.ArrayList;
public class CO2FromWasteTester
{
    public static void main(String[]args){
        ArrayList<CO2FromWaste> waste = new ArrayList<CO2FromWaste>();

        waste.add(new CO2FromWaste(1, true, true, true, true));
        waste.add(new CO2FromWaste(3, true, false, true, true));
        waste.add(new CO2FromWaste(4, false, false, false, false));
        waste.add(new CO2FromWaste(1, true, true, true, true));
        waste.add(new CO2FromWaste(1, true, true, true, true));

        CO2FromWaste wasteRecord;
        for (int index = 0; index < waste.size(); index++){
            wasteRecord = waste.get(index);
            wasteRecord.calcGrossWasteEmission();
            wasteRecord.calcWasteReduction();
            wasteRecord.calcNetWasteReduction();
        }
        System.out.println("                   Household Waste Recycled                    Total    Pounds of CO2    Net");
        System.out.println(" Index    People    Paper     Plastic     Glass     Cans     Emission     Reduction    Emission ");

        for (int index = 0; index < waste.size(); index ++)
        {
            wasteRecord = waste.get(index);
            System.out.printf("%3d %9d %10s %10s %10s %10s %12.2f %10.2f %10.2f%n",index,wasteRecord.getNumPeople(),wasteRecord.getPaper(), wasteRecord.getPlastic(), wasteRecord.getGlass(),wasteRecord.getCans(),wasteRecord.getGrossWasteEmission(),wasteRecord.getWasteReduction(),wasteRecord.getNetWasteReduction());
        }
    }
}

输出应该像表一样读取,并在标题下包含正确的数据。 对于第一行,输出应为

0   1   true   true   true   true   1018.00   422.00   596.00 

但它的内容是

0  1   true   true   true   true    422.00   422.00   422.00

启动总排放部分有一些错误,而且该部分应该相当简单,因为它需要做的就是将给出的人数乘以1018。 我不知道该怎么做,并希望得到一些帮助。

3 个答案:

答案 0 :(得分:1)

这是问题所在:

public void calcNetWasteReduction(){
    netWasteReduction = grossWasteEmission = wasteReduction;    
}

相当于:

public void calcNetWasteReduction(){
    grossWasteEmission = wasteReduction;    
    netWasteReduction = grossWasteEmission;
}

换句话说,它不应该修改grossWasteEmission。我怀疑你想要:

public void calcNetWasteReduction(){
    netWasteReduction = grossWasteEmission - wasteReduction;    
}

换句话说,将第二个=改为-

不清楚为什么你有三个独立的方法,说实话 - 为什么不在构造函数中执行计算?或者在getter方法中(完全删除中间字段)?

此外,考虑制作废物减量类型(纸张,玻璃,塑料)的枚举,并考虑它们EnumSet。如果将它们作为单独的参数保留,我强烈建议您将名称更改为更传统的Java(paper而不是Paper等)并使用{{1而不是if (paper)等。一般来说,避免对布尔常量进行显式检查。

答案 1 :(得分:0)

问题在于:

for (int index = 0; index < waste.size(); index++){
    wasteRecord = waste.get(index);
    wasteRecord.calcGrossWasteEmission();
    wasteRecord.calcWasteReduction();
    wasteRecord.calcNetWasteReduction(); // <== netWasteReduction = grossWasteEmission = wasteReduction;
}

答案 2 :(得分:0)

您只需要替换下面的'='符号即可。由于这条线,你得到的所有三个值相等。 : - )

public void calcNetWasteReduction(){     netWasteReduction = grossWasteEmission = wasteReduction;

希望这有帮助