用两个重载的方法求数组的平均值(java方法)
答案:1 悬赏:70 手机版
解决时间 2021-04-04 21:46
- 提问者网友:沉默菋噵
- 2021-04-04 06:53
用两个重载的方法求数组的平均值(java方法)
最佳答案
- 五星知识达人网友:鱼忧
- 2021-04-04 07:56
这些简单的题应该自己多动手写写!!
public class ArrayAverage{
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1, 2, 3, 4, 5, 6};
double[] b = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
int avg1 = average(a);
double avg2 = average(b);
System.out.println("int:"+avg1);
System.out.println("double:"+avg2);
}
public static int average(int[] array){
int sum = 0;
for(int i = 0;i sum += array[i];
}
return (sum/6);
}
public static double average(double[] array){
int sum = 0;
for(int i = 0;i sum += array[i];
}
return (sum/6);
}
}
public class ArrayAverage{
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1, 2, 3, 4, 5, 6};
double[] b = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
int avg1 = average(a);
double avg2 = average(b);
System.out.println("int:"+avg1);
System.out.println("double:"+avg2);
}
public static int average(int[] array){
int sum = 0;
for(int i = 0;i sum += array[i];
}
return (sum/6);
}
public static double average(double[] array){
int sum = 0;
for(int i = 0;i sum += array[i];
}
return (sum/6);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯