Copy code
public static double a1(double[]... arrays) {
double sum = 0
int count = 0
for (double[] array : arrays) {
for (double value : array) {
sum += value
count++
}
}
return sum / count
}
然后可以使用以下代码来调用该方法,并将结果赋给对应的变量:
Copy code
double a2, b2, c2, d2
double a[] = {90, 85, 66, 51, 69, 78, 88, 94}
double b[] = {68, 88, 81, 90, 65, 74, 64, 46}
double c[] = {68, 90, 85, 88, 68, 70}
double d[] = {60}
a2 = b2 = c2 = d2 = a1(a, b, c, d)
这样就可以使用一次方法调用计算所有数组的平均值了。
package testimport java.util.List
public interface Average{
public double getAverage(List<Double>list)
}
package test
import java.util.List
public class First implements Average {
public double getAverage(List<Double>list) {
double sum = 0
for(int i=0i<链哗察list.size()i++){
sum += list.get(i).doubleValue()
}
return sum / list.size()
}
}
package test
import java.util.Collections
import java.util.List
public class Second implements Average {
public double getAverage(List<Double>list) {
Collections.sort(list)
double sum = 0
for(int i=1i<list.size()-1i++){
sum += list.get(i).doubleValue()
}
return sum / (list.size() - 2)
}
}
package test
import java.util.ArrayList
import java.util.List
public class Test {
public static void main(String[] args){
List<Double>list = new ArrayList<Double>()
list.add(50d)
list.add(48d)
list.add(20d)
list.add(51d)
list.add(36d)
Average first = new First()
Average second = new Second()
System.out.println(first.getAverage(list))
System.out.println(second.getAverage(list))
}
}
为了测试,我只输入了5个数,棚茄录入的代码网芦唤上很多,算法给出了,祝你好运。
public class MaxMin {
public static void main(String[] args) {
Integer[] inte = {12,23,54,75,86}
int max = inte[0]
int min = inte[0]
int average = 0
int sum = 0
for(int i=0i<inte.lengthi++){
if(max<inte[i]){
max = inte[i]
}
if(min>春早睁inte[i]){
min = inte[i]
}
sum += inte[i]
}
average = sum/inte.length
System.out.println("最大值----"+max)
System.out.println("扒岁最小值----"睁基+min)
System.out.println("平均值----"+average)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)