Q:

Adding fractions in Java

I am trying to make a simple loop which adds fractions
double sum = 0;
for (int i=1; i<=20; i++) {
    sum += 1/(i+2);
}
return sum;

but it seems like Java doesn't understand fractions? Can someone tell me what's wrong?

A:

it's doing integer division, cast it double
sum += (double)1 / (i + 2);

