0
Java Arrays is not being compute all, it is computed 1 by 1
Ok so I need to get the average of my array, but the average is given 1 by 1. here is my code: public static void main(String[] args) { // TODO code application logic here int[] arr = new int[] {10,20,30,40,15,25,35,50,45,60}; JOptionPane.showMessageDialog(null,"List of Numbers:"+Arrays.toString(arr)); average(arr); } public static int average(int[] arr){ int su = 0 ; for (int i = 0; i < arr.length; i++) { su = su+arr[i]; double avr = (double)su/arr.length; JOptionPane.showMessageDialog(null,"Average: "+avr); } return su; } }
1 Antwort
0
Change the position of your 3rd last brace( } )
place it after su=su+arr[i];.Hence code will look like :-
public static void main(String[] args) {
// TODO code application logic here
int[] arr = new int[]
{10,20,30,40,15,25,35,50,45,60};
JOptionPane.showMessageDialog(null,"List of Numbers:"+Arrays.toString(arr));
average(arr);
}
public static int average(int[] arr){
int su = 0 ;
for (int i = 0; i < arr.length; i++)
{
su = su+arr[i];
}
double avr = (double)su/arr.length;
JOptionPane.showMessageDialog(null,"Average: "+avr);
return su;
}
}