代码是必需的(不要手动打印出来,写一个FOR循环来做)
打印所有白色家具
打印阵列中所有家具的总成本
这是我的代码:
public class FurnitureDemo
{
public static void main (String[] args)
{
Furniture [] f = new Furniture [6];
f[0] = new Chair (100, 1.56, "white", 1.5, 3);
f[1] = new Chair (55, 2.52, "black", 2, 3);
f[2] = new Table (300, 2.53, "gray", 4, 3, 2.6);
f[3] = new Table (220, 3.5, "brown",5, 3, 2.5);
f[4] = new Table (250,4.5, "white", 5, 4, 2.6);
f[5] = new Round_Table (500,5.5,"white",1.5, 2.4);
for(int i=0;i<f.length;i++)
{
System.out.println(f[i].toString());
}
}
}
答案 0 :(得分:1)
假设您的家具/子类型家具对象有Color和getPrice方法的getter方法:
int costOfAllFurniture=0;
for(int i=0;i<f.length;i++)
{
if(f[i].getColor().equals("White")){
System.out.println(f[i].toString()); //would print all white furniture
}
costOfAllFurniture+=f[i].getPriceOfFurniture();
}
答案 1 :(得分:1)
print all white furniture
你需要在循环中使用if
operator:
if (f[i].getColor().equals("White"))
打印阵列中所有家具的总成本
这是计算总费用的方法。
//outside the loop
int totalCost = 0;
//inside the loop
totalCost += f[i].getCost();