gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:47 26 4
gpt4 key购买 nike

我的作业是计算一年中产生的二氧化碳量一个家庭,并比较回收如何减少其二氧化碳足迹。程序中有两个类: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。我不知道从这里该怎么做,希望得到一些帮助。

最佳答案

这就是问题:

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

这相当于:

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

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

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

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

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

此外,考虑对减少浪费的类型(PAPER、GLASS、PLASTIC)进行枚举,并采用其中的 EnumSet。如果您确实将它们保留为单独的参数,我强烈建议您将名称更改为更常规的 Java(paper 而不是 Paper 等),并使用 if (paper) 而不是 if (paper == true) 等。一般来说,避免对 boolean 常量进行显式检查。

关于JAVA对象数组程序输出错误(初级),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27214836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com