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

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

Divide integer elements in an array

问题

  1. 如何将数组中的所有输入整数进行分割
  2. 我有这个名为division的方法它只计算我输入的整数的和如何将它们进行分割
  3. public class Arithmetic {
  4. public int division(int[] n) {
  5. int quotient = 0;
  6. for (int num : n) {
  7. quotient = quotient / num;
  8. }
  9. return quotient;
  10. }
  11. public static void main(String[] args) throws IOException {
  12. Arithmetic a = new Arithmetic();
  13. Scanner sc = new Scanner(System.in);
  14. System.out.println("1: Division");
  15. System.out.print("Enter selection here: ");
  16. int choice = sc.nextInt();
  17. switch (choice) {
  18. case 1:
  19. System.out.print("Enter no. of elements you want in array: ");
  20. int numOfElements = sc.nextInt();
  21. System.out.println("Enter elements in the array ~ ");
  22. if (sc.hasNextInt()) {
  23. int arraysInput[] = new int[numOfElements];
  24. for (int i = 0; i < numOfElements; i++) {
  25. arraysInput[i] = sc.nextInt();
  26. }
  27. System.out.println("Quotient: " + a.division(arraysInput));
  28. }
  29. }
  30. }
  31. }
英文:

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?

  1. public class Arithmetic {
  2. public int division(int[] n) {
  3. int quotient = 0;
  4. for (int num : n) {
  5. quotient = quotient / num;
  6. }
  7. return sum;
  8. }
  9. public static void main(String[] args) throws IOException {
  10. Arithmetic a = new Arithmetic();
  11. Scanner sc = new Scanner(System.in);
  12. System.out.println(&quot;1: Division&quot;);
  13. System.out.print(&quot;Enter selection here: &quot;);
  14. int choice = sc.nextInt();
  15. switch (choice) {
  16. case 1:
  17. System.out.print(&quot;Enter no. of elements you want in array: &quot;);
  18. int numOfElements = sc.nextInt();
  19. System.out.println(&quot;Enter elements in the array ~ &quot;);
  20. if (sc.hasNextInt()) {
  21. int arraysInput[] = new int[numOfElements];
  22. for (int i = 0; i &lt; numOfElements; i++) {
  23. arraysInput[i] = sc.nextInt();
  24. }
  25. System.out.println(&quot;Quotient: &quot; + a.division(arraysInput));
  26. }
  27. }
  28. }
  29. }

答案1

得分: 0

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

参考:

  1. public int division(int[] n) {
  2. int quotient = n[0];
  3. for (int i = 1; i < n.length; i++) {
  4. quotient = quotient / n[i];
  5. }
  6. return quotient;
  7. }
  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:

  1. public int division(int[] n) {
  2. int quotient = n[0] ;
  3. for (int i = 1; i &lt; n.length ; i++) {
  4. quotient = quotient / n[i];
  5. }
  6. 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

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

  1. public int division(int[] n) {
  2. int quotient = n[0];
  3. for (int i = 1; i < n.length; i++) {
  4. quotient = quotient / n[i];
  5. }
  6. return quotient;
  7. }

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

  1. public class Arithmetic {
  2. public int division(int[] n) {
  3. int quotient = n[0];
  4. for (int i = 1; i < n.length; i++) {
  5. quotient = quotient / n[i];
  6. }
  7. return quotient;
  8. }
  9. public static void main(String[] args) throws IOException {
  10. Arithmetic a = new Arithmetic();
  11. Scanner sc = new Scanner(System.in);
  12. // 测试除法
  13. // System.out.println("Divison of {10,5,1} : " + a.division(new int[]{10,5,1}));
  14. System.out.println("1: Division");
  15. System.out.print("在此输入选择:");
  16. int choice = sc.nextInt();
  17. switch (choice) {
  18. case 1:
  19. System.out.print("输入你想要的数组元素数量:");
  20. int numOfElements = sc.nextInt();
  21. System.out.println("输入数组元素 ~ ");
  22. if (sc.hasNextInt()) {
  23. int arraysInput[] = new int[numOfElements];
  24. for (int i = 0; i < numOfElements; i++) {
  25. arraysInput[i] = sc.nextInt();
  26. }
  27. System.out.println("商:" + a.division(arraysInput));
  28. }
  29. }
  30. }
  31. }
英文:

You need to change your division method with this

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

The whole class looks like with some test code :

  1. public class Arithmetic {
  2. public int division(int[] n) {
  3. int quotient = n[0] ;
  4. for (int i = 1; i &lt; n.length ; i++) {
  5. quotient = quotient / n[i];
  6. }
  7. return quotient;
  8. }
  9. public static void main(String[] args) throws IOException {
  10. Arithmetic a = new Arithmetic();
  11. Scanner sc = new Scanner(System.in);
  12. // Test Division
  13. // System.out.println( &quot;Divison of {10,5,1} : &quot; + a.division(new int[]{10,5,1}));
  14. System.out.println(&quot;1: Division&quot;);
  15. System.out.print(&quot;Enter selection here: &quot;);
  16. int choice = sc.nextInt();
  17. switch (choice) {
  18. case 1:
  19. System.out.print(&quot;Enter no. of elements you want in array: &quot;);
  20. int numOfElements = sc.nextInt();
  21. System.out.println(&quot;Enter elements in the array ~ &quot;);
  22. if (sc.hasNextInt()) {
  23. int arraysInput[] = new int[numOfElements];
  24. for (int i = 0; i &lt; numOfElements; i++) {
  25. arraysInput[i] = sc.nextInt();
  26. }
  27. System.out.println(&quot;Quotient: &quot; + a.division(arraysInput));
  28. }
  29. }
  30. }
  31. }

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:

确定