该程序需要遍历数组列表以将所有整数加在一起,但出现“无法取消引用int”错误。我认为int不能在数组列表中使用,但这就是指南程序中的方式,我不确定该怎么做。第一种方法需要计算总人数,然后乘以1018的平均排放量。请帮助我。
这段代码已经给我了,当他们给您代码时,您不会更改他们给您的内容,而只需添加缺少的内容。
方法类别
// This class instantiates CO2FromWaste(8.11) objects with 8 private instance variables.
// It contains three mutator methods to calculate the pounds of CO2: in total emissions,
// emission reductions, and net emissions. It contains getter methods for each private
// instance variables. Private instance variables include myNumPeople, myPaper, myPlastic,
// myGlass, myCans, myEmissions, myReduction, and myNetEmissions.
//
public class CO2FromWaste
{
private int myNumPeople;
private boolean myPaper, myPlastic, myGlass, myCans;
private double myEmissions, myReduction, myNetEmissions;
//
// Constructor for objects of type CO2FromWaste
// @param numPeople number of people in a household
// @param paper whether or not paper is recycled
// @param plastic whether or not plastic is recycled
// @param glass whether or not glass is recycled
// @param cans whether or not cans are recycled
CO2FromWaste(int numPeople, boolean paper, boolean plastic, boolean glass, boolean cans)
{
myNumPeople = numPeople;
myPaper = paper;
myPlastic = plastic;
myGlass = glass;
myCans = cans;
myEmissions = 0.0;
myReduction = 0.0;
myNetEmissions = 0.0;
}
//
// Mutator method to calculate the total emissions, without any recycling (no parameters)
这是第一个问题的来历。
public void calcGrossWasteEmission()
{
double sum = 0;
for(int i = 0; i< myNumPeople.size(); i++)
{
sum += myNumpeople.get(i);
}
myEmissions = sum 1018;
}
//
// Mutator method to calculate the emission reduction from recycling (no parameters)
public void calcWasteReduction()
{
if(myPaper)
{
myReduction += (184.0 myNumPeople);
}
// fill in rest of method here //
}
//
// Mutator method to calculate the net emissions (no paramters)
public void calcNetWasteReduction()
{
// fill in rest of method here //
}
//
// Getter method to return the number of people (no paramters)
public int getNumPeople()
{
return myNumPeople;
}
//
// Getter method to return paper's recycled status (true or false) (no paramters)
public boolean getPaper()
{
return myPaper;
}
//
// Getter method to return glass's recycled status (true or false) (no paramters)
public boolean getGlass()
{
return myGlass;
}
//
// Getter method to return plastic's recycled status (true or false) (no paramters)
public boolean getPlastic()
{
return myPlastic;
}
//
// Getter method to return cans' recycled status (true or false) (no paramters)
public boolean getCans()
{
return myCans;
}
//
// Getter method to return the total emissions (no paramters)
public double getEmissions()
{
return myEmissions;
}
//
// Getter method to return the reduction amount (no paramters)
public double getReduction()
{
return myReduction;
}
//
// Getter method to return the net emissions (no paramters)
public double getNetEmissions()
{
return myNetEmissions;
}
}
测试人员类别
/**
* @purpose: Calculate the CO2 from household waste 8.11
*
* @author:
* @version:
*/
import java.util.ArrayList;
public class CO2FromWasteTester
{
public static void main(String[] args)
{
ArrayList<CO2FromWaste> cO2 = new ArrayList<CO2FromWaste>();
// adding households
cO2.add(new CO2FromWaste(1,true,true,true,true));
cO2.add(new CO2FromWaste(3,true,false,true,true));
cO2.add(new CO2FromWaste(4,false,false,false,false));
cO2.add(new CO2FromWaste(1,true,true,true,true));
cO2.add(new CO2FromWaste(1,true,true,true,true));
for(CO2FromWaste dataRecord : cO2)
{
dataRecord.calcGrossWasteEmission();
dataRecord.calcWasteReduction();
dataRecord.calcNetWasteReduction();
}
System.out.println("| | | | Pounds of CO2 |");
System.out.println("| | | Household Waste Recycled | Total | | Net |");
System.out.println("| Index | People | Paper | Plastic | Glass | Cans | Emission | Reduction | Emission |");
System.out.println("|-------|--------|----------|----------|---------|---------|------------|-------------|------------|");
CO2FromWaste dataRecord;
for(int index = 0; index < cO2.size(); index ++)
{
dataRecord = cO2.get(index);
System.out.printf("| %1d | %2d | %-5b | %-5b | %-5b | %-5b | %8.2f | %7.2f | %8.2f |%n", index, dataRecord.getNumPeople(), dataRecord.getPaper(), dataRecord.getPlastic(), dataRecord.getGlass(), dataRecord.getCans(), dataRecord.getEmissions(), dataRecord.getReduction(), dataRecord.getNetEmissions());
}
}
}
最佳答案
myNumPeople
是int
,但是您试图将其当作List
使用(myNumpeople.get(i)
,myNumpeople.size()
等)。
您应该更改myNumPeople
的类型。