我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*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;
}
}
导入论坛查看(38)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing