日期:2014-05-20  浏览次数:20634 次

求教:求数组平均数问题.(新手)
目的:求数组中每次循环的result值和平均数,不知道哪里出了错?
class   average2
{
  public   static   void   main   (String   args[])
{
  double   nums[]   ={10.1,50.2,10.3,10.4,10.0};
  double   result=0;
  int   i=0;
  int   x=1;
  for   (;i <5;i++)
  for   (;x <6;x++)

{result=result+nums[i];
System.out.println( "result   is:   "+result);


  System.out.println( "average   is   "   +   result/x);}

}
}

------解决方案--------------------
class Average2
{
public static void main (String args[])
{

double nums[] ={10.1,50.2,10.3,10.4,10.0};
double result=0.0;


for (int i=1;i <6;i++){


result+=nums[i-1];
System.out.println( "result is: "+result);
System.out.println( "average is " + result/i);
}

}
}