将数组中的整数元素进行分割

huangapple go评论81阅读模式
英文:

Divide integer elements in an array

问题

如何将数组中的所有输入整数进行分割
我有这个名为division的方法它只计算我输入的整数的和如何将它们进行分割

public class Arithmetic {
    public int division(int[] n) {
        int quotient = 0;
        for (int num : n) {
            quotient = quotient / num;
        }
        return quotient;
    }

    public static void main(String[] args) throws IOException {

        Arithmetic a = new Arithmetic();
        Scanner sc = new Scanner(System.in);

        System.out.println("1: Division");
        System.out.print("Enter selection here: ");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                System.out.print("Enter no. of elements you want in array: ");
                int numOfElements = sc.nextInt();
                System.out.println("Enter elements in the array ~ ");
                if (sc.hasNextInt()) {
                    int arraysInput[] = new int[numOfElements];
                    for (int i = 0; i < numOfElements; i++) {
                        arraysInput[i] = sc.nextInt();
                    }
                    System.out.println("Quotient: " + a.division(arraysInput));
                }
        }
    }
}
英文:

How do you divide all the input integers in an array?
I have this method called division. It only gets the sum of the integers I've input. How do divide them?

public class Arithmetic {
public int division(int[] n) {
    int quotient = 0;
    for (int num : n) {
        quotient = quotient / num;
    }
    return sum;
}

public static void main(String[] args) throws IOException {

    Arithmetic a = new Arithmetic();
    Scanner sc = new Scanner(System.in);

    System.out.println(&quot;1: Division&quot;);
    System.out.print(&quot;Enter selection here: &quot;);
    int choice = sc.nextInt();
    switch (choice) {
        case 1:
            System.out.print(&quot;Enter no. of elements you want in array: &quot;);
            int numOfElements = sc.nextInt();
            System.out.println(&quot;Enter elements in the array ~ &quot;);
            if (sc.hasNextInt()) {
                int arraysInput[] = new int[numOfElements];
                for (int i = 0; i &lt; numOfElements; i++) {
                    arraysInput[i] = sc.nextInt();
                }
                System.out.println(&quot;Quotient: &quot; + a.division(arraysInput));
            }
    }
  }
}

答案1

得分: 0

被接受的答案不可行。遗憾的是,我没有足够的“声望”来发表评论。

参考:

public int division(int[] n) {
    int quotient = n[0];
    for (int i = 1; i < n.length; i++) {
        quotient = quotient / n[i];
    }
    return quotient;
}
  1. 如果值数组为空或为null - 抛出异常。
  2. 如果数组包含零(DivisionByZeroException)
  3. 如果数组只有一个值,则返回的值不是商(除非假设是value/1?)
    • 吹毛求疵...但返回值应该是double或float吗?
英文:

The accepted answer will not work. Unfortunately I don't have enough "rep" to post comments.

for reference:

public int division(int[] n) {
int quotient = n[0] ;
for (int i = 1; i &lt; n.length ; i++) {
    quotient = quotient / n[i];
}
return quotient;

}

  1. if the array of values is empty or null - exception thrown.
  2. if the array contains a zero (DivisionByZeroException)
  3. if the array has only one value, the returned value is not the quotient (unless the assumption is value/1?)
    • nitpicking... but shouldn't the return be a double or a float?

答案2

得分: 0

你需要使用以下方式更改你的除法方法:

public int division(int[] n) {
    int quotient = n[0];
    for (int i = 1; i < n.length; i++) {
        quotient = quotient / n[i];
    }
    return quotient;
}

整个类的外观类似于以下测试代码:

public class Arithmetic {

    public int division(int[] n) {
        int quotient = n[0];
        for (int i = 1; i < n.length; i++) {
            quotient = quotient / n[i];
        }
        return quotient;
    }

    public static void main(String[] args) throws IOException {

        Arithmetic a = new Arithmetic();
        Scanner sc = new Scanner(System.in);

        // 测试除法
        // System.out.println("Divison of {10,5,1} : " + a.division(new int[]{10,5,1}));

        System.out.println("1: Division");
        System.out.print("在此输入选择:");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                System.out.print("输入你想要的数组元素数量:");
                int numOfElements = sc.nextInt();
                System.out.println("输入数组元素 ~ ");
                if (sc.hasNextInt()) {
                    int arraysInput[] = new int[numOfElements];
                    for (int i = 0; i < numOfElements; i++) {
                        arraysInput[i] = sc.nextInt();
                    }
                    System.out.println("商:" + a.division(arraysInput));
                }
        }
    }
}
英文:

You need to change your division method with this

public int division(int[] n) {
    int quotient = n[0] ;
    for (int i = 1; i &lt; n.length ; i++) {
        quotient = quotient / n[i];
    }
    return quotient;
}

The whole class looks like with some test code :

public class Arithmetic {

    public int division(int[] n) {
        int quotient = n[0] ;
        for (int i = 1; i &lt; n.length ; i++) {
            quotient = quotient / n[i];
        }
        return quotient;
    }

    public static void main(String[] args) throws IOException {

        Arithmetic a = new Arithmetic();
        Scanner sc = new Scanner(System.in);

        
        // Test Division
        // System.out.println( &quot;Divison of {10,5,1} : &quot; + a.division(new int[]{10,5,1}));


        System.out.println(&quot;1: Division&quot;);
        System.out.print(&quot;Enter selection here: &quot;);
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                System.out.print(&quot;Enter no. of elements you want in array: &quot;);
                int numOfElements = sc.nextInt();
                System.out.println(&quot;Enter elements in the array ~ &quot;);
                if (sc.hasNextInt()) {
                    int arraysInput[] = new int[numOfElements];
                    for (int i = 0; i &lt; numOfElements; i++) {
                        arraysInput[i] = sc.nextInt();
                    }
                    System.out.println(&quot;Quotient: &quot; + a.division(arraysInput));
                }
        }
    }
}

huangapple
  • 本文由 发表于 2020年10月17日 00:10:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64392757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定