我刚刚学到了两件事 - 1)如何在JAVA中使用省略号,即如何定义函数的变长参数列表。 以下是一个演示上述概念的程序。如何在命令行参数中使用省略号?
public class variable
{
public static void main(String[] args)
{
int d1=2;
int d2=3;
int d3=4;
int d4=5;
System.out.print(average(d1,d2,d3));
System.out.print(average(d1,d2));
System.out.print(average(d1,d2,d3,d4));
}
public static int average(int... numbers)
{
int total=0;
for(int i:numbers)
{
total+=i;
}
return total/numbers.length;
}
}
2)如何使用命令行参数。下面是使用这个concept-
public class argument
{
public static void main(String[] args)
{
if(args.length!=3)
{
System.out.println("Please provide valid 3 inputs to add them all");
}
else
{
int first = Integer.parseInt(args[0]);
int second = Integer.parseInt(args[1]);
int third = Integer.parseInt(args[2]);
System.out.println((first+second+third));
}
}
}
现在程序...我的问题是,如何在其中,我想通过命令行给输入程序中使用省略号?
假设我想通过命令行参数添加3个数字,但我的朋友想要添加5个数字在一起。我如何使用省略号来迎合我和我朋友的要求?
你可以写一个方法,它需要一个'int []'并在其中加入数字? –
这已被问过。请参阅[http://stackoverflow.com/questions/7574543/how-to-pass-console-arguments-to-application-in-eclipse][1] [1]:HTTP:// stackoverflow.com/questions/7574543/how-to-pass-console-arguments-to-application-in-eclipse – Kathir
标题中的“eclipse”标签和“eclipse”是“省略号”的拼写错误,不是吗? –