统计信息

  • 访问数:8783
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q08

2008-04-14 22:15:10

天气: 晴朗 心情: 高兴

/*filename:P6Q08
 * Name:Wang Chaoran
 * Description:
 * (Averaging an array) Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] array);
public static double average(double[] array);
Use {1, 2, 3, 4, 5, 6} and {6.0, 4.4, 1.9, 2.9, 3.4, 3.5} to test the methods.
*/
class P6Q08{
  public static void main(String[] args){
    int[] test1={1, 2, 3, 4, 5, 6};
    double[] test2={6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
    System.out.println("The average of the integer array is: "+average(test1));
    System.out.println("The average of the double array is: "+average(test2));
  }
  public static int average(int[] array){
    int sum = 0;
    for(int i=0;i<array.length;i++)
      sum+=array[i];
    return sum/array.length;
  }
  public static double average(double[] array){
    double sum = 0;
    for(int i=0;i<array.length;i++)
      sum+=array[i];
    return sum/array.length;
  }
}
加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd