我在下面运行这个java代码,它应该给我34.5的最大值。相反,它是2.0。诡异的!谁能告诉我这段代码有什么问题?谢谢。
public class VarargsDemo {
public static void main(String args[]){
printMax(34.5, 3, 5, 6, 2);
printMax(new double[]{1,2,3});
}
public static void printMax(double...numbers){
if (numbers.length==0){
System.out.println("No argument passed");
return;
}
double result=numbers[0];
for (int i=1; i<numbers.length;i++)
result=numbers[i];
System.out.println("the max value is "+result);
}
}
输出:
the max value is 2.0
the max value is 3.0