java吧 关注:1,296,364贴子:12,833,217
  • 4回复贴,共1

各位帮看下为什么运行不了?

只看楼主收藏回复

interface ComputerWeight
{  public double computerWeight();
}
class Television implements ComputerWeight
{  public double computerWeight()
  {
return 50;
}
}
class Computer implements ComputerWeight
{public double computerWeight(){
return 45;
}}
class WashMachine implements ComputerWeight
{  public double computerWeight(){
return 30;
}
}
class Car
{  ComputerWeight[] goods;
   double totalWeight=0;
   Car(ComputerWeight[] goods)
   { this.goods=goods;
   }
   public double getTotalWeight()
   {  totalWeight=0;
      for(int i=0;i<goods.length;)
      {totalWeight=totalWeight+goods[i].computerWeight();      
      
      }return totalWeight;
   }
}
public class Road 
{  public static void main(String args[])
   {  ComputerWeight[] goodsOne=new ComputerWeight[50],
                   goodsTow=new ComputerWeight[22];
   for(int i=0;i<goodsOne.length;i++)
   {  if(i%3==0)
   goodsOne[i]=new Television();
   else if(i%3==1)
   goodsOne[i]=new Computer();
   else if(i%3==2)
   goodsOne[i]=new WashMachine();
   }
   for(int i=0;i<goodsTow.length;i++)
   {  if(i%3==0)
   goodsTow[i]=new Television();
   else if(i%3==1)
   goodsTow[i]=new Computer();
   else if(i%3==2)
   goodsTow[i]=new WashMachine();
   }
   Car 大货车=new Car(goodsOne);
   System.out.println("大货车装载的货物重量:"+大货车.getTotalWeight());
   Car 小货车=new Car(goodsTow);
   System.out.println("小货车装载的货物重量:"+小货车.getTotalWeight());
   
   }
}


1楼2009-10-18 21:39回复
    新手,见谅


    2楼2009-10-18 21:42
    回复
      2026-01-31 00:02:21
      广告
      不感兴趣
      开通SVIP免广告
      for(int i=0;i<goods.length; i++){ totalWeight=totalWeight+goods[i].computerWeight();
      }
      //死循环了


      3楼2009-10-18 22:00
      回复
        疏忽了。谢谢。


        4楼2009-10-18 22:29
        回复
          • 219.159.104.*
          不好意思,我也是新手,刚也在弄这个程序
          请问一下,为什么这句是totalWeight=totalWeight+goods[i].computerWeight();
          goods[i].computerWeight()这个用法我不太懂


          8楼2009-11-10 22:40
          回复