Winston Gutkowski wrote:
Carmenia Bona wrote:This is what I'm getting:...
Carmenia,
A really good idea when you run into problems like this is to StopCoding (←click).
Then, you have two choices (both of which require a pencil and LOTS of paper):
1. Go through your code and write down, for each iteration of the loop, the values of each variable. An alternative is to print them all out with System.out.println() statements.
2. Forget the code altogether and write down in English (or your native language) the exact steps required to get the result you want; and then check those steps against what you've actually coded.
Personally, I prefer number 2; but the best time to do it is before you write any Java code at all.
HIH
Winston
K. Tsang wrote:What are you getting?
Ramesh Kumar Muthukumar wrote:average = (double) sum / upperbound;
this also works.
Ramesh Kumar Muthukumar wrote:
average = sum / upperbound;
both the 'sum' and 'upperbound' are int values and the result also will be int and then it is converted into double.
that is why you get 50.0 instead of 50.5.
There are many ways to get the expected output. one of them is changing the upperbound and lowerbound datatype to double.