Methods with array of parameters in Java
Forums:
static double getAvg(double... values) {
if (values.length == 0) {
return 0;
}
double sum = 0;
for(double value: values){
sum += value;
}
return sum/values.length;
}
double avg = getAvg(1, 2, 3, 4);
//avg is 2.5
- Log in to post comments