+ 1

Hi friends,do you know why the average exam points of each student are plus constantly itself? Please help mešŸ™šŸ™

public class ac{ public static void main(String[]args){ int[][]a={{62,98},{86,53},{64,87},{94,85},{96,65},{64,76}}; int sum=0; for(int i=0;i<a.length;i++){ int j=0; sum+=a[i][j]; } System.out.println("The average points of chinese exam="+ sum/a.length); for(int i=1;i<a.length;i++){ int j=1; sum+=a[i][j]; } System.out.println("The average of math examt="+ sum/a.length); int average=0; for(int i=0;i<a.length;i++){ for(int j=0;j<=1;j++){ average+=a[i][j];} System.out.println("The average points of " + (i+1) + "student are "+ average/2); } } } The problem come out over herešŸ‘†šŸ‘†šŸ‘†šŸ‘†šŸ‘†šŸ‘†šŸ‘†,the last for loop of the program.

22nd Jun 2018, 12:33 AM
Hunt
Hunt - avatar
2 Answers
+ 2
I have the answer. You have average incrementing without resetting it for each student. So, just replace the last for loops with the following: int average=0; for(int i=0;i<a.length;i++){ average=0; //I added in this, it is what was needed. for(int j=0;j<=1;j++){ average+=a[i][j];} System.out.println("The average points of " + (i+1) + "student are "+ average/2); }
22nd Jun 2018, 1:42 AM
Andre Daniel
Andre Daniel - avatar
+ 1
You are right,thank you!
22nd Jun 2018, 3:23 AM
Hunt
Hunt - avatar