这个程序需要迭代一个数组列表来将所有整数添加到一起,但是我得到一个“int not be dereferenced”错误。我认为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;
}
}
Tester Class
/**
* @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());
}
}
}
答案 0 :(得分:1)
myNumPeople
是int
,但您尝试使用它,就好像它是List
myNumpeople.get(i)
,myNumpeople.size()
等。 ..)。
您应该更改myNumPeople
的类型。
答案 1 :(得分:1)
int
和boolean
是原始数据类型;他们不是对象,所以他们无法进入List
。相反,请使用Integer
和Boolean
。