我知道我做这个完全错误的atm,我是新编码,我知道我做什么都是错的。尽管如此。重载方法问题
创建两个重载方法,它们返回数组数组的平均值。一种方法是
public static double average (int[] array)
其他方法的标题是
public static double average (double[] array)
我我想要调用这两种,并打印平均主要方法。第一个数组使用{1,2,3,4,5}来测试接受整数的平均方法,并使用这个数组{6.0,5.0,4.0,3.0,2.0,1.0}来测试接受双精度的平均方法作为参数。
现在我的代码,可能看起来很干净。我真的不知道。
public class methodss
{
public static void main(String[] args)
{
//invoke the average int method
System.out.println("Average with int: " + average);
//invoke the average double method
System.out.println("Average with double: " + average);
public static double average (int[] array)
{
int sum = 0, average = 0;
array[5] = {1,2,3,4,5}
for (int i = 0; i < 10; ++i){
sum+=array[i];
average = sum/5;
return average;
}
}
public static double average (double[] array)
{
int sum = 0, average2 = 0;
array[6] = {6.0, 5.0, 4.0, 3.0, 2.0, 1.0};
for (int x = 0; x < 10; ++x){
sum+=array[x];
average = sum/6;
return average;
}
}
}
你可以做methodss.average(新INT [0])和methodss.average(新双[0]); –
如果你知道你完全错了,那么停下来学习正确的方法。 –