So I get how arrays work. I get how methods work. I don't seem to understand how you combine the two.
Say for example in this program I intend to get the sum of the elements in an array. I know it's possible with only one method but for learning's sake, lets say I'd use another method. So far I've got this:
Now the error is here:
Why doesn't that work?
/>
Say for example in this program I intend to get the sum of the elements in an array. I know it's possible with only one method but for learning's sake, lets say I'd use another method. So far I've got this:
import java.util.Random; public class Main { public static void main(String args[]){ Random Rand = new Random(); int arNum[] = new int[10]; int ctr=0; int Answer; for(ctr=0;ctr<10;ctr++){ arNum[ctr] = ((Rand.nextInt(6)) + 1); } Answer = getSum(arNum[ctr]); } public static int getSum(int x[]){ int count=0; int sum = 0; for(count=0;count<=x.length;count++){ sum += x[count]; } return sum; } }
Now the error is here:
Answer = getSum(arNum[ctr]);
Why doesn't that work?
