返回数字序列之和并以系列数组作为参数的Java方法

时间:2021-07-15 12:37:25

I have tried this:

我试过这个:

public class NumberSeries {
    public static void main(String[] args) {
        // this line of code is not correct
        SumOfNumbers( int nums [{23,44,12,33}]);
    }

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

I am failing to call SumOfNumbers method correctly. What am I doing wrong?

我没能正确调用SumOfNumbers方法。我究竟做错了什么?

3 个解决方案

#1


3  

What you are trying to do is declaring an in-line inline array.

您要做的是声明一个内联行内联数组。

SumOfNumbers( int nums [{23,44,12,33}]);

That should be

那应该是

SumOfNumbers( new int [] {23,44,12,33}); // in-line passing

or even clear

甚至清楚

int[] nums = new int[] {23,44,12,33}; // declaring
SumOfNumbers( nums); //passing

You are not declaring it properly.

你没有正确地宣布它。

#2


1  

Not passing the array in correct way, modify code as following

没有以正确的方式传递数组,修改代码如下

int[] nums ={23,44,12,33};
SumOfNumbers( nums);

#3


1  

  int [] nums={23,44,12,33};

      SumOfNumbers( nums);

You can use this code in main method.

您可以在main方法中使用此代码。

#1


3  

What you are trying to do is declaring an in-line inline array.

您要做的是声明一个内联行内联数组。

SumOfNumbers( int nums [{23,44,12,33}]);

That should be

那应该是

SumOfNumbers( new int [] {23,44,12,33}); // in-line passing

or even clear

甚至清楚

int[] nums = new int[] {23,44,12,33}; // declaring
SumOfNumbers( nums); //passing

You are not declaring it properly.

你没有正确地宣布它。

#2


1  

Not passing the array in correct way, modify code as following

没有以正确的方式传递数组,修改代码如下

int[] nums ={23,44,12,33};
SumOfNumbers( nums);

#3


1  

  int [] nums={23,44,12,33};

      SumOfNumbers( nums);

You can use this code in main method.

您可以在main方法中使用此代码。