paul
paul

Reputation: 4487

Java method that returns sum of number series and takes series array as parameter

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?

Upvotes: 3

Views: 220

Answers (3)

tarikfasun
tarikfasun

Reputation: 324

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

      SumOfNumbers( nums);

You can use this code in main method.

Upvotes: 1

Rahul Yadav
Rahul Yadav

Reputation: 1513

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

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

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 3

Related Questions