简体   繁体   English

(JAVA) 数组中的整数相加

[英](JAVA) Addition of integers in an Array

I'm new to java programming so please excuse any misunderstandings or misinterpretations.我是 Java 编程的新手,所以请原谅任何误解或误解。 I need to write a program with 2 methods, the first method declares an array of integers as shown and a variable that is the sum of this array then prints out the sum.我需要用 2 个方法编写一个程序,第一个方法声明一个整数数组,如图所示,一个变量是这个数组的总和,然后打印出总和。

The second method is where my addition takes place.第二种方法是我添加的地方。 I've declared a variable "sum" equal to 0 so my addition can work, and a for loop that adds all integers based on the array length, returning the variable sum back to the main method.我已经声明了一个等于 0 的变量“sum”,所以我的加法可以工作,还有一个 for 循环,它根据数组长度添加所有整数,将变量 sum 返回给 main 方法。 (From my current understanding of my program) (根据我目前对我的程序的理解)

So far this does not work and I get 3 errors.到目前为止,这不起作用,我收到 3 个错误。 One from my main method "cannot find symbol - inputArray" cannot find symbol in my for loop for "arr.length" and cannot find symbol in my " sum += arr[i]" Could someone please explain and possibly assist with why I am getting these errors.我的主要方法中的一个“找不到符号 - inputArray”在我的 for 循环中找不到符号“arr.length”并且在我的“sum += arr[i]”中找不到符号有人可以解释并帮助解释为什么我收到这些错误。 Thank you for your time.感谢您的时间。

public static void main(String[] args){
    int arr[] = {1,2,3,4,5};
    int sum = sumArray(inputArray);
    System.out.println("The sum is: "+sum);
}
public static int sumArray(int[] inputArray){
    int sum = 0;
    int i;
    for (i = 0; i < arr.length; i++){
        sum += arr[i];
        return sum;
    }   

}

The variable name is inputArray .变量名称是inputArray Also the return statement should be outside of the loop: return语句也应该在循环之外:

public static int sumArray(int[] inputArray){
    int sum = 0;
    for (int i = 0; i < inputArray.length; i++){
        sum += inputArray[i];
    }   
    return sum;
}

You can also use for each to find the sum.您还可以使用for each来求和。 Find the code below找到下面的代码

public static int sumArray(int[] arr){
    int sum = 0;
    for(int i:arr) {
        sum+=i;
    }
    return sum;   
}

Return statement should be outside of for loop. Return 语句应该在 for 循环之外。 When the return statement is in the loop sumArray method returns the value of the first element of inputArray.当 return 语句在循环中时 sumArray 方法返回 inputArray 第一个元素的值。 Below I also added for each loop and replaced name arr with parameter name inputArray.下面我还为每个循环添加并用参数名称 inputArray 替换了名称 arr。

public static int sumArray(int[] inputArray){
int sum = 0;
for (int element : inputArray){
    sum += inputArray[i];
}
return sum;
}

In java-8 you can use streams like this:在 java-8 中,您可以使用这样的流:

public static int sumArray(int[] arr) {
    int sum = IntStream.of(arr).sum();
    return sum;
}

Output: The sum is: 15.输出:总和为:15。

It's in the package java.util.stream它在包 java.util.stream 中

I think this should solve your problem:我认为这应该可以解决您的问题:

public static void main(String[] args){
   int arr[] = {1,2,3,4,5};
   int sum = sumArray(arr);
   System.out.println("The sum is: "+ sum);
}

public static int sumArray(int[] inputArray){
   int sum = 0;
   for (int i = 0; i < inputArray.length; i++){
       sum += inputArray[i];
       return sum;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM