+ 1

Can someone send the solution to this question about Annual Revenue in java under looping over statements , my code wont run

The given code declares an array that holds the monthly revenues for a company for a year. You need to calculate the average monthly revenue for the year. For that, calculate the sum of the revenue for all the months and divide it by the number of items in the array. -------------------------------------------- Hint: You can find the number of items in the array using the length property. As the array is of type double, output the result as a double. My code : class program{ public static void main(String[] args){ double[] revenue={10000,12000,13500,9000,5600,6700,23000,7850,8900,10050,12050,13550}; double sum=0; for(int i=0;i<revenue.length;i++){ sum=sum+revenue[i]; } double result =sum/revenue.length; System.out.println(result);     } }

30th Aug 2023, 3:17 AM
Andrew Coutinho
Andrew Coutinho - avatar
2 Antworten
+ 6
Andrew Coutinho , when running your code in code coach, the compiler claims 'some illegal characters`, which are a so called `NO-BREAK SPACE` . this may be caused by a copy / paste action. > to correct the code, replace all leading `spaces` (at the beginning of all lines) by regular spaces. >>> Edited: the illegal characters (4×) are only in the second last line, just before the closing curly bracket. >>> also use the correct input numbers as given in the code from sololearn: double[] revenue = {88750, 125430, 99700, 14500, 158000, 65000, 99000, 189000, 210000, 42000, 165800, 258900}; doing these 2 amendments, the code runs properly in code coach.
30th Aug 2023, 12:35 PM
Lothar
Lothar - avatar
+ 2
Hi , correct code public class Program { public static void main(String[] args) { double[] revenue = {88750, 125430, 99700, 14500, 158000, 65000, 99000, 189000, 210000, 42000, 165800, 258900}; double sum = 0.0; for (double amount : revenue) { sum += amount; } double average = sum / revenue.length; System.out.println(average); } }
21st Feb 2024, 7:36 AM
Irfan Alam
Irfan Alam - avatar