编辑:暂时失去了我的大脑。道歉。在java中添加实例变量的值
如果我有一个名为numThings的实例变量(每个框中的事物数)的n个Box对象。每个盒子中的numThings是随机的。我如何计算每个盒子中的所有数字并将它们加在一起?
public class Box {
int numThings = RandomHelper.nextIntFromTo(0, 10);
class Box (int numThings) {
this.numThings = numThings;
}
//set and get numThings code here
List<Box> fullBoxes = new ArrayList<Box>();
if (this.numThings > 0) {
fullBoxes.add(this);
}
//Not sure where to go with this. I want to know the total number of things in all the boxes combined
public void countNumThings() {
for (Box box: fullBoxes){
box.getNumThings()
}
}
}
创建一个名为总和变量,然后加getNumThings()在循环中。谢谢, – csmckelvey 2015-03-03 01:16:20