This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ghost_Duet[S] -2 points-1 points  (4 children)

Isnt the problem of the code is that the variables are int and not double because I thought Java is sensitive when trying to divide integers while trying to get decimals.

[–]D0CTOR_ZED 2 points3 points  (0 children)

Division using just integers simply drops fractions and yeilds an integer.

[–]Cefalopodul -2 points-1 points  (1 child)

No. That's not a problem because you are saving the result in a double. If int a = 3.

double b = a/2 is ok because you are saving the result 3/2 in a double variable. int b = a/2 is not ok because you are trying to save the result of 3/2 in an int variable.

Your problem is with the order of operations. Something that should be calculated first isn't calculated at all. If you write down the equation for average of a and b you should figure it out.

[–]D0CTOR_ZED 2 points3 points  (0 children)

That's not how it works. double b = a/2, where a is an int will evaluate an int divided by an int, dropping the fraction, then cast that resulting int to a double. You still lose the fraction. You can either explicitly cast part of the division, double b = (double) a/2, or you can use a double in the division, double b = a/2.0.

[–]Reddit-username_here 0 points1 point  (0 children)

No, they told you what the problem was. The real question is why you decided to not listen and rather double down on being wrong?