How to return values from methods that contain arrays in parameters in Java?
•
Java
I have this code, but it doesn't work!
public class Trial { public static void main (String [] args) { int average; int m = 0; int [] nums = {1,6,8,9,10,60,72}; average = getAverage(int [] nums); } public static int getAverage(int [] a) { int sum = 0; for(int i=0; i<a.length; i++) sum = sum +a[i]; int avg = sum/a.length; return avg; } }
What's the problem? I need to get the average of the array by calling the method of calculating the average
Solution
Change method call:
average = getAverage(nums);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码