简体   繁体   English

如何在Java中创建一个包含可变数量参数的“除法”函数

[英]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.

相关问题 java:我如何创建一个支持任意数量参数的函数? - java: how can i create a function that supports any number of parameters? 如何在 Java 中创建包含运算符的变量? - How to create a variable that contains an Operator in Java? Java变量函数参数 - Java variable function parameters java - 为包含类型参数的类型创建一个比较器 - java - Create a Comparator for a type that contains type parameters 如何有条件地将Java中的参数传递给一个采用可变数量参数的方法? - How to conditionally pass parameters in Java to a method that takes a variable number of arguments? 如何从 java 反射中的可变数量参数中获取方法 - How to getMethod from a variable number of parameters in java reflection 如何将可变数量的参数从JSP页面传递到Java控制器 - How to pass variable number of parameters from jsp page to the java controller 如何使用以股息和除数为参数的递归创建int []除法? - How to create an int[] division method using recursion with dividend and divisor as parameters? 如何使用来自 Java 的参数运行 VBS 函数并将结果分配给变量 - How to run VBS function with parameters from Java and assign result to variable 具有可变数量参数的Java“虚拟”方法 - Java “Virtual” method with variable number of parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM