[英]how to create a "Division" function which contains variable number of parameters in Java
I want to ask a question how to create a "Division" function which contains variable number of parameters in Java?我想问一个问题,如何在 Java 中创建一个包含可变数量参数的“除法”函数? Just Like As I did for sum method:
就像我对 sum 方法所做的那样:
public static int sum(int ... x)
{
int sum=0;
for(int i : x)
{
sum=sum+i;
}
return sum;
}
Well, the difference is that you're gonna have to provide a number to divide as parameter:好吧,不同之处在于您必须提供一个数字作为参数进行除法:
public static int div(int div, int ... x)
{
for(int i : x)
{
if(i > 0) div=div/i;
}
return div;
}
Be careful to test if i is greater than 0, otherwise the compiler will throw an error, since you cannot divide by 0.小心测试i是否大于 0,否则编译器会抛出错误,因为你不能被 0 除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.