I wrote just as hinted. (9/5) first - the answer was wrong.
then remove the bracket as in 9/5. again the last condition was unapproved because the answer was wrong.
Then I wrote 9/5.0 as per the solution and only then was the answer right.
Anonymous #10749591
Level 3
How is it wrong.
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
2 June 2021, 05:48
There are several primitive data types defined in Java. Some, like int and long, work with integers, others with floating point numbers (double, float).With mathematical operations like multiplication, addition or subtraction with all these types no problem is expected (in general), but when it comes to division one has to be careful.
If you divide ints like 9 / 5 the result is 1, because there are no decimal places. The CPU only examines how often the 5 fits into the 9. If you want to have more exact results with floating point part, you have to convert one of the operands into a floating point number. The compiler then converts the other one automatically like e.g.
What does not work is
Then the division is int based and the result of 9/5 you cast into a double (with multiplying it with 1.0)
The same happens here as well, an int based division with a result of 1 and the result, the 1, you cast to double.
+3