尝试对数组中的元素求和时的奇怪输出(使用方法)(weird output when trying to sum elements in array (using method))

编程入门 行业动态 更新时间:2024-10-28 05:16:17
尝试对数组中的元素求和时的奇怪输出(使用方法)(weird output when trying to sum elements in array (using method))

我正在制作一个添加数组中所有元素的方法。 我从main中的testArray1获取数组。 输出是“[I @ 15d6c7”

public class arrayExercises{ public int ArraySum(int[] array){ int sum = 0; for(int i = 0; i < array.length; i++){ sum += array[i]; } return sum; } public static void main(String[] args){ int[] testArray1 = {1,2,3,4}; System.out.println(testArray1); } }

I am making a method that adds all the elements in the array. I am getting the array from the testArray1 in main. The output is "[I@15d6c7"

public class arrayExercises{ public int ArraySum(int[] array){ int sum = 0; for(int i = 0; i < array.length; i++){ sum += array[i]; } return sum; } public static void main(String[] args){ int[] testArray1 = {1,2,3,4}; System.out.println(testArray1); } }

最满意答案

您打印出testArray1的字符串表示形式而不是其元素的总和。 我想你想改变你的主要方法如下:

public static void main(String[] args){ int[] testArray1 = {1,2,3,4}; System.out.println(ArraySum(testArray1)); }

You're printing out the string representation of testArray1 instead of the sum of its elements. I think that you want to change your main method as follows:

public static void main(String[] args){ int[] testArray1 = {1,2,3,4}; System.out.println(ArraySum(testArray1)); }

更多推荐

本文发布于:2023-07-15 22:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对数   使用方法   组中   元素   奇怪

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!