gpt4 book ai didi

java - 如何在 ArrayList 中使用整数和 boolean 值

转载 作者:行者123 更新时间:2023-12-02 04:01:30 25 4
gpt4 key购买 nike

该程序需要迭代数组列表以将所有整数加在一起,但我收到“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 的类型。

关于java - 如何在 ArrayList 中使用整数和 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842633/

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