如果不确定数组中参数的个数,可以使用java中的可变参数 语法格式:
---------------------------------------------------------------------------------------------------------------------------------
修饰符 方法名(数据类型...参数名){}
public class Demo1 { public static void main(String[] args) { printMax(10,2,3,4,5,6,7); } //输出最大值 public static void printMax(double...numbers){ double result = numbers[0]; if (numbers.length==0){ System.out.println("您输入的参数为空!请重新输入!"); }else { for (int i = 0; i <numbers.length ; i++) { if (result<numbers[i]){ result=numbers[i]; } } System.out.println("最大值是:"+result); System.out.println(numbers.length); } } }
---------------------------------------------------------------------------------------------------------------------------------
运行结果:
---------------------------------------------------------------------------------------------------------------------------------