The arithmetic mean is a value that is often used in statistics. A hospital's average temperature is precisely calculated using the formula for the arithmetic mean. And now we've come to the critical part: write a program that takes numbers from the keyboard, sums them, and then calculates the mean until the user enters the number -1.
Arithmetic mean
- 8
Locked
Comments (35)
- Popular
- New
- Old
You must be signed in to leave a comment
ajinkya bharate
25 May 2021, 11:01
make sum variable a double
+2
KARMA GURUNG
21 February 2021, 12:45
hint: use while loop, check the entered no. with (if). if it is -1 then, display the mean otherwise keep on adding double is the key
0
Sinisa
20 February 2021, 10:44
This task's description is either not properly described, or the proposed solution is flawed.
The solution provided by the CodeGym expects user to eventually input -1. In the task's description, it is an condition, not an obligation. Hence, if the user doesn't input -1, the program returns an error, ie., never finishes.
So, either change the requirements and say, not IF the user enters -1 but WHEN, or state the number of user inputs, so that the program can return an output in case the user doesn't enter -1.
0
Jac
22 September 2020, 01:46
I'm getting NaN as an output. Any help?
0
Toni Fields
24 January 2021, 02:59
Not sure if you figured this one out yet, but you should move your meanNum initialization to after your while loop. In your while loop, remove the print statement. This statement should come after the while loop and after you declare the meanNum variable. Everything else is fine.
0
ImDevin
5 April 2021, 14:51
Nan is what you get when you divide 0.0 by 0.0, as it is in line 20. Prior to line 20, there is no increase in neither of the numbers, so you have to fix the order of operations first.
+1
Erin Armstrong
8 February 2020, 19:30
ok i got the solution to the arithmetic one but i literally don't understand why.
basically i was getting 2.16777777777
then i added {} after my if and closed it from my break
and it worked.......
0
ъуъ
25 September 2020, 10:46

+3
Cristea Ovidiu-Valentin
19 November 2019, 22:35
"Casting" is used to get a double from an integer division :
1. double d = (double)5 / 20;
2. double v = (double)5 / (double) 20;
3. double v = 5 / (double) 20;
or implicit casting :
double d = num * 1.0 / denom;
Just learned that and it is useful ,.
+2
Firdous Shaikh
6 November 2019, 07:26
com/codegym/task/task05/task0507/Solution.java:11: error: cannot find symbol
Scanner s = new Scanner(System.in);
^
symbol: class Scanner
Why am i getting this error
location: class com.codegym.task.task05.task0507.Solution
0
Roman
7 November 2019, 05:50
You should import Scanner class.
+1
Raj Mishra Java Developer at Sopra banking softwa
17 October 2019, 10:28
I think you found the error as the task been closed ?
0
Lakshmanan Subramanian
10 October 2019, 01:23
Please help me to identify the bug
0
Laurence Chadwell
22 September 2019, 02:01
It's funny how making everything a double makes a big difference, lmao!
+3
Łukasz Szajek
30 September 2019, 10:45
My thoughts exactly :D
Thinking why 14/5 gives 2.0 istead of 2.8 gave me a headache...
0